简体   繁体   中英

Convert date time with AM PM to 24 hour date format

How to convert 10/25/2014 14:00 PM to 2014-10-25 14:00:00 in php?

date('Y-m-d H:i:s', strtotime("10/25/2014 14:00 PM"));

returns

1970-01-01 03:00:00
$date = "10/25/2014 14:00 PM"; //Here is the date 24 hours format with am/pm
$date = substr($date, 0, -2); //Removed the am/pm from date


echo date('Y-m-d H:i:s', strtotime($date)); //then convert it to mysql date format

Note: If you use this for date column means replace it data type to int and save unix time stamp which have more comfortable if year limited to 1970 to year 2038. For reason not to use more than 2038 read here

10/25/2014 14:00 PM您的输入日期格式不正确,您使用的是12小时格式,所以时间将是10/25/2014 02:00 PM

I don't know what the origin of that date is (how it came to that), but you can use DateTime classes for this:

$raw_date = '10/25/2014 14:00 PM';
// to disregard that PM escape it in the format
$new_date = DateTime::createFromFormat('m/d/Y H:i \P\M', $raw_date);
echo $new_date->format('Y-m-d H:i:s'); // 2014-10-25 14:00:00

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