DateTime::createFromFormat 这是一个错误吗?

DateTime::createFromFormat is this a bug?

我想用这种格式“26 January | 03h”解析日期,但它不适用于 \DateTime::createFromFormat。这是一个错误吗?还是我做错了什么!?

<?php
$format = 'j F | H\h';
$nowStr =  (new \DateTime())->format($format);
$newDate = \DateTime::createFromFormat($format, $nowStr);
if ($newDate === false){
    echo "What the F***!?";
}
else{
    echo "My mistake!";
}

Test 自己动手吧。

不是错误。竖线(|)字符是日期的有效字符格式,因此您需要对其进行转义:

| Resets all fields (year, month, day, hour, minute, second, fraction and timzone information) to the Unix Epoch if they have not been parsed yet

例如:

$format = 'j F \| H\h';

测试一下 here。 :)