I have a string of time such as: 6:00 AM, 7:30 PM, etc. It can easily be changed to 6:00:00 AM, 7:30:00 PM, etc as well.
I'm looking for a way to quickly convert this time into a 24 hour format, e.g 7:30 PM = 19:30:00.
Is there any PHP or MySQL function which has this capability, or do I have to devise my own?
SELECT STR_TO_DATE( '2019-06-19 10:00 pm', '%Y-%m-%d %h:%i %p' ); We have to use the date for getting the proper time in 24 hours.
$format_time = str_replace(" AM", "", $time, $count); if ($count === 0){ $format_time = strstr($time, ' PM', true); $format_time = ...... }
MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
STR_TO_DATE( `column_here`, '%l:%i %p' )
Read more here.
Edited '%l:%M %p'
to '%l:%i %p'
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