Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get AM/PM from a datetime in PHP [duplicate]

I have a date time in a variable. My format is 08/04/2010 22:15:00. I want to display this like 10.15 PM. How to do this in PHP?

like image 759
Testadmin Avatar asked Aug 04 '10 10:08

Testadmin


People also ask

How to use date_ format in PHP?

Example. $date=date_create("2013-03-15"); echo date_format($date,"Y/m/d H:i:s");

How can change time in 24 hour format in PHP?

$time = '23:45'; echo date('g:i a', strtotime($time));

What is Strtotime PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


2 Answers

You need to convert it to a UNIX timestamp (using strtotime) and then back into the format you require using the date function.

For example:

$currentDateTime = '08/04/2010 22:15:00'; $newDateTime = date('h:i A', strtotime($currentDateTime)); 
like image 79
John Parker Avatar answered Sep 20 '22 12:09

John Parker


$dateString = '08/04/2010 22:15:00'; $dateObject = new DateTime($dateString); echo $dateObject->format('h:i A'); 
like image 40
Mark Baker Avatar answered Sep 22 '22 12:09

Mark Baker