【发布时间】:2018-08-03 13:22:39
【问题描述】:
我似乎无法让 DateTime::createFromFormat() 在 PHP 7.0.26 中工作
我的代码
date_default_timezone_set('America/New_York');
$t = '2018-02-23T11:29:16.434Z';
echo '$t: ' . json_encode($t) . PHP_EOL;
$f = DateTime::RFC3339_EXTENDED;
echo '$f: ' . json_encode($f) . PHP_EOL;
echo 'createFromFormat: ' . json_encode(DateTime::createFromFormat($f, $t)) . PHP_EOL;
echo 'getLastErrors: ' . json_encode(DateTime::getLastErrors()) . PHP_EOL;
$t = '2018-02-23T11:29:16.434+00:00';
echo '$t: ' . json_encode($t) . PHP_EOL;
echo 'createFromFormat: ' . json_encode(DateTime::createFromFormat($f, $t)) . PHP_EOL;
echo 'getLastErrors: ' . json_encode(DateTime::getLastErrors()) . PHP_EOL;
输出:
$t: "2018-02-23T11:29:16.434Z"
$f: "Y-m-d\\TH:i:s.vP"
createFromFormat: false
getLastErrors: {"warning_count":0,"warnings":[],"error_count":2,"errors":{"20":"The format separator does not match","21":"The timezone could not be found in the database"}}
$t: "2018-02-23T11:29:16.434+00:00"
createFromFormat: false
getLastErrors: {"warning_count":0,"warnings":[],"error_count":2,"errors":{"20":"The format separator does not match","21":"The timezone could not be found in the database"}}
我注意到“v”未列在DateTime::createFromFormat() 的格式参数值中——但据说我应该能够使用包含“v”的 const DateTime::RFC3339_EXTENDED。它还说this const was added in version 7.0
【问题讨论】:
-
@CBroe - 不,我没有看到它在页面下方。谢谢 - 将其作为答案,以便我接受。
-
对亚历克斯的回答很满意,他甚至更多地研究了细节,所以你不妨接受那个;-)
-
是的。 “v”部分甚至不在文档中,因此无法解析回来。它似乎应该是“你”。去图吧。
标签: php datetime php-7 datetime-format