如何在php中将10/25/2014 14:00 PM
转换为2014-10-25 14:00:00
?
date('Y-m-d H:i:s', strtotime("10/25/2014 14:00 PM"));
返回
1970-01-01 03:00:00
我不知道这个日期的来源是什么(它是如何产生的),但您可以使用DateTime
类来实现这一点:
$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
$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
注:如果将其用于日期列,则意味着将其数据类型替换为int并保存unix时间戳,如果年份限制在1970年到2038年,则该时间戳会更方便。出于不使用超过2038的原因,请阅读此处
2014年10月25日下午14:00您输入的日期格式不正确,您使用的是12小时格式,因此时间将为2014年10日下午02:00