For example, I have a time in this format:
eg. 09:15 AM 04:25 PM 11:25 AM
How do I convert it to :
09:15 16:25 23:25
Currently my code is :
$format_time = str_replace(" AM", "", $time, $count); if ($count === 0){ $format_time = strstr($time, ' PM', true); $format_time = ...... }
However, it seems there are some easier and more elegant way to do this?
$time = '23:45'; echo date('g:i a', strtotime($time));
How do I fit the above sample in my case? Thanks.
Changing 12-Hour Time to 24-Hour Time. Use 00:00 to signify midnight in 24-hour time. Rather than using "12:00" twice in a 24-hour period, like in 12-hour time, 24-hour time uses "00:00" for the midnight hour.
Try with this
echo date("G:i", strtotime($time));
or you can try like this also
echo date("H:i", strtotime("04:25 PM"));
If you use a Datetime format see http://php.net/manual/en/datetime.format.php
You can do this :
$date = new \DateTime(); echo date_format($date, 'Y-m-d H:i:s'); #output: 2012-03-24 17:45:12 echo date_format($date, 'G:ia'); #output: 05:45pm
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