我以前曾经DateTime::createFromFormat()
将字符串解析为日期,但今天我发现了一个问题,我从这个调用中得到了 null。
echo $date_string;
$test = DateTime::createFromFormat('M d Y, H:i:s T', trim($date_string));
echo $test->format('Y-m-d H:i:s');
输出:
Oct 16 2013, 15:45:02 CEST
( ! ) Fatal error: Call to a member function format() on a non-object in (...) on line 51
第 51 行是我调用的最后一行format()
。
那么,我错过了什么吗?提前致谢!
DateTime::getLastErrors()
按要求输出:
(
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 4
[errors] => Array
(
[3] => Unexpected data found.
[11] => Unexpected data found.
[22] => Unexpected data found.
[36] => The timezone could not be found in the database
)
)
@whizzkid要求的捕获:
DateTime::__construct(): Failed to parse time string (Oct 16 2013, 10:00:00 CEST) at position 3 (&): Unexpected character