Reputation: 267067
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?
Upvotes: 6
Views: 19383
Reputation: 1
Try the below query,
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. You can add any static date before the time, above solution worked for me :)
Upvotes: 0
Reputation: 1241
Do Like That
SELECT STR_TO_DATE('08:35 PM', '%l:%i %p');
ANS: '20:35:00'
Upvotes: 0
Reputation: 80639
STR_TO_DATE( `column_here`, '%l:%i %p' )
Read more here.
Edited '%l:%M %p'
to '%l:%i %p'
Upvotes: 15
Reputation: 3816
Try strtotime, which retrieves the Unix timestamp for any english date/time format. If it doesn't work out of the box, maybe add an arbitrary day in front of the time string you pass along. Then you can use strftime to convert the timestamp into a 24h format.
Also I've heard of a DateTime
object, which may be able to handle both conversions as well.
Upvotes: 1