Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP adding AM/PM to a time string

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"
like image 437
user982124 Avatar asked Dec 26 '22 03:12

user982124


2 Answers

you have to try this one:

echo date("g:i A", strtotime($bookingTime));
like image 113
Akif Hussain Sayyed Avatar answered Dec 27 '22 17:12

Akif Hussain Sayyed


You can try something like this as well

$d = new DateTime('08:15:00'); 
echo $d->format( 'g:i A' );
like image 33
Ram Sharma Avatar answered Dec 27 '22 17:12

Ram Sharma