In MySQL, how you convert time in AM / PM to minute after midnight format. EG:- 1. 8:40 AM will be 520 2. 9:00 PM will be 1260
Thanks
First convert the string to a date with STR_TO_DATE()
, then do the calculation with HOUR()
and MINUTE()
functions:
SELECT HOUR(STR_TO_DATE('8:40 AM', '%l:%i %p')) * 60 +
MINUTE(STR_TO_DATE('8:40 AM', '%l:%i %p')) AS minutes
Results in:
minutes
---------
520
And ...
SELECT HOUR(STR_TO_DATE('9:00 PM', '%l:%i %p')) * 60 +
MINUTE(STR_TO_DATE('9:00 PM', '%l:%i %p')) AS minutes
Results in:
minutes
---------
1260