简体   繁体   中英

Format CURRENT_TIMESTAMP to AM & PM

Today im working with PHP and MySQL and i store Dates in an Timestamp Field into MySQL, But by default is in 24/h Format, If anyone Know how can i Determine this hour to AM or PM Example:

14:05 -> 2:05PM
02:05 -> 2:05AM

Many Thanks for your time.

你可以使用DATE_FORMAT()

SELECT DATE_FORMAT(hour_column, '%r');

No, no, no. A "timestamp" value might mean "11:00am" (if you're in New York), but the SAME value would ALSO mean "4:00pm" (if you're in Greenwich, England).

A timestamp VALUE is independent of the TIME ZONE you're located in.

Having said that, you want to convert a date time STRING (in 24/h format), into a DIFFERENT date time string (convert to 12/meridian format).

Easily done from either MySQL or PHP: whichever you prefer.

Here are the PHP date/time format functions and corresponding format strings:

http://php.net/manual/en/function.strtotime.php

http://www.php.net/manual/en/datetime.formats.time.php

And here are the MySQL reference pages:

http://dev.mysql.com/doc/refman/5.5/en/datetime.html

http://www.w3schools.com/sql/func_date_format.asp

DateTime would be your friend.

First, if your php config dosen't already set it up for you, set your default timezone by:

date_default_timezone_set('XXXX');

XXXX stand for a value out of the List of supported timezones

initialize your date by:

$date = new DateTime();

To get your timestamp you can use now:

$timestamp = $date->getTimestamp();

As earlier said the output (format) depends on your timezone. You set your default timezone earlier but you can change it all the time, as you need.

To format the date you can use this:

$12h = $date->format( 'h:i:s a' );
$24h = $date->format( 'H:i:s a' );

To read more about DateTime look at the DateTime class manual .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM