PHP DateTime::RFC3339_EXTENDED 坏了吗?

Is PHP DateTime::RFC3339_EXTENDED broken?

我似乎无法 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() -- but supposedly I should be able to use the const DateTime::RFC3339_EXTENDED which includes a "v". It also says that this const was added in version 7.0

的格式参数值中

正如 CBroe 指出的那样,this 是适合您的解决方案。您应该使用 Y-m-d\TH:i:s.uP 而不是 DateTime::RFC3339_EXTENDED,即 Y-m-d\TH:i:s.vP:

$date = DateTime::createFromFormat("Y-m-d\TH:i:s.uP", "2018-02-23T11:29:16.434Z"); //works

我真的去看看为什么会这样,这就是我的发现。

有一个closed bug requesting support for RFC3339 with milliseconds. The bug author created a pull request to add this feature. But whilst he created a RFC3339_EXTENDED constant for the format function, he did not add the support for createFromFormat. If you take a look here,不支持v选项(也就是毫秒)。所以是的。

是的,显然它只在 PHP 7.3.0 中得到修复,请参阅此 https://3v4l.org/4nMi4