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?
Example. $date=date_create("2013-03-15"); echo date_format($date,"Y/m/d H:i:s");
$time = '23:45'; echo date('g:i a', strtotime($time));
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.
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));
$dateString = '08/04/2010 22:15:00'; $dateObject = new DateTime($dateString); echo $dateObject->format('h:i A');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With