Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting an AM/PM time to 24 hours format using PHP or MySQL?

Tags:

date

php

mysql

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?

like image 680
Ali Avatar asked Sep 12 '12 23:09

Ali


People also ask

How to convert AM pm to 24 hours in MySQL?

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.

How to convert AM pm time to 24 hours in php?

$format_time = str_replace(" AM", "", $time, $count); if ($count === 0){ $format_time = strstr($time, ' PM', true); $format_time = ...... }

What is time format in MySQL?

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.


1 Answers

STR_TO_DATE( `column_here`, '%l:%i %p' )

Read more here.

EDIT

Edited '%l:%M %p' to '%l:%i %p'

like image 191
hjpotter92 Avatar answered Sep 28 '22 05:09

hjpotter92