我需要转换Oct 24, 2011 03:16 PM到2011-10-24 15:16:00在php。
我知道这是可能的,为每个月创建一个变量"$jan = 01, $feb = 02,等等",然后使用"explosion"按空间拆分信息,并以正确的顺序重新排列。
但是有没有更简单的方法呢?
$string_date = strtotime("Oct 24, 2011");
$date_object = strtotime($string_date);
$formatted = date("Y-m-d h:i:s", $date_object);
查看date()函数以获得更多格式化选项
函数strtotime()
是一个非常强大的日期解析器。使用它来获取传入日期的UNIX时间戳,并使用date()
函数以所需格式输出日期。
<?php
$ts = strtotime("Oct 24, 2011 03:16 PM");
$date = date("Y-m-d H:i:s", $ts);
?>
echo date("Y-m-d H:i:s", strtotime("Oct 24, 2011 03:16 PM"));
日期(Y-m-d H:我:年代",strtotime(2011年10月24日03:16点));
其他答案中提到的strtotime()选项是最简单的方法,但不要爱上所有日期字符串->数字转换的函数。它可以也将会在某个时候搞砸。如果您确切地知道日期字符串的格式,那么使用更可靠的
$date = DateTime::createFromFormat('M j', Y', "Oct 24,2011");