PHP adding AM/PM to a time string
Asked Answered
K

4

5

I'm getting a time value from a database call in the following format:

HH:MM:SS

Here are some examples:

08:15:00
22:45:00

I need to however convert these and display them, using the above examples, as follows:

8:15 AM
10:45 PM

and so on. I've been playing around with the strtotime options but everything I've tried has failed so far, and I'm not sure if that's the correct function to even use. I need go from to:

$bookingTime = "08:15:00"

to:

$bookingTime = "8:15 AM"
Kaolack answered 27/10, 2014 at 12:20 Comment(1)
what have you coded yet ?Might
U
8

you have to try this one:

echo date("g:i A", strtotime($bookingTime));
Unvoice answered 27/10, 2014 at 12:31 Comment(0)
C
3

You can try something like this as well

$d = new DateTime('08:15:00'); 
echo $d->format( 'g:i A' );
Cockleshell answered 27/10, 2014 at 12:27 Comment(0)
C
1

try this:

$currentDateTime = '08:15:00';
$newDateTime = date('h:i A', strtotime($currentDateTime));

echo $newDateTime;

result: 08:15 AM

possible duplicate of this question

Cabinetwork answered 27/10, 2014 at 12:24 Comment(0)
W
-1

it's working try it

 $bookingTime = "22:45:00";

  list($hour, $minute, $second) = explode(':', $bookingTime);
  $date_time1 = mktime($hour, $minute, $second);
  echo date('g:i A',$date_time1);
Wandawander answered 27/10, 2014 at 12:38 Comment(1)
i try it , it's compliantly working as per requirementWandawander

© 2022 - 2024 — McMap. All rights reserved.