Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting mysql TIME from 24 HR to AM/PM format

I want to display the TIME field from my mysql table on my website, but rather than showing 21:00:00 etc I want to show 8:00 PM. I need a function/code to do this or even any pointers in the right direction. Will mark the first reply with some code as the correct reply.

like image 406
Ali Avatar asked Oct 06 '08 01:10

Ali


People also ask

How do I add AM PM in MySQL?

But if you want to store AM/PM you need to use varchar datatype. I suggest you to use ->format(); while you get the timestamp data. This will convert the date time format as your wish. For now, as you want to display AM/PM after time, use it like: ->format('g:i A'); This will show time like 10:28 PM .

How do I convert 12 hour format to 24 hour format in MySQL?

The DATE_FORMAT() will do this for you. %r and %T are 12 hour and 24 hour time formats respectively.

How do I change from 24 hour format to 12 hour format in PHP?

Similarly, H will give you the hour in 24-hour format with leading zeros, but h will give you the hour in 12-hour format with leading zeros.

What is the format of time 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.


2 Answers

Check this out: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

I'd imagine you'd want date_format().

Example: DATE_FORMAT($date, "%r")

like image 151
Steve Klabnik Avatar answered Sep 21 '22 17:09

Steve Klabnik


Show the date & time data in AM/PM format with the following example...

SELECT DATE_FORMAT(`t`.`date_field`,'%h:%i %p') AS `date_field` FROM `table_name` AS `t`

OR

SELECT DATE_FORMAT(`t`.`date_field`,'%r') AS `date_field` FROM `table_name` AS `t`

Both are working properly.

like image 20
JDGuide Avatar answered Sep 22 '22 17:09

JDGuide