Dean Ambrose
Dean Ambrose

Reputation: 193

convertime datetime object am-pm

I have a date time object as:

$startTime = new DateTime();
$startTime->setTime($weekTimeArr[1][0]["hour"], $weekTimeArr[1][0]["minute"]);
$startTime->format('h:i a');
$endTime->setTimezone(new DateTimeZone('IST'));

Now this thing works fine and I get values as 05:00AM, 02:00AM.... But how can I tell this logic to return values in PM when a particular condition is met.

Example :

if(a == true) {
   $startTime = new DateTime();
   $startTime->setTime($weekTimeArr[1][0]["hour"], $weekTimeArr[1][0]["minute"]);
   $startTime->format('h:i a');
   $endTime->setTimezone(new DateTimeZone('IST'));
}

now if a==true then it should return 5PM instead of 5AM.

Upvotes: 0

Views: 73

Answers (2)

user6679664
user6679664

Reputation:

it is unclear on what is the value of $weekTimeArr[1][0]["hour"] but in order to convert a given time from AM to PM you need to do this

$startTime->modify('+12 hours')->format('h:i a');

Upvotes: 0

Code Lღver
Code Lღver

Reputation: 15603

Here is the solution. You have to add the 12 in the hours if your condition is true. Code is below:

if(a == true) {
   $hour = $weekTimeArr[1][0]["hour"] + 12;
   $startTime = new DateTime();
   $startTime->setTime($hour, $weekTimeArr[1][0]["minute"]);
   $startTime->format('h:i a');
   $endTime->setTimezone(new DateTimeZone('IST'));
}

Upvotes: 1

Related Questions