为什么 DateTime::createFromFormat() 在我的第二个示例中失败并返回布尔值?
当我运行它时,第一个被正确地创建为日期.第二个失败,返回一个 boolean
所以我无法格式化.时间是否超出范围?
When I run this the first one is correctly created into a date. The second one fails, returning a boolean
and so I cannot format. Is the time out of range?
//works correctly
$startDate = "2015-05-06 10:49:20.637133";
$start = DateTime::createFromFormat('Y-m-d h:m:s.u',$startDate);
echo $start->format('m/d/y');
//doesn't work correctly
$startDate = "2015-05-12 15:49:06.821289";
$start = DateTime::createFromFormat('Y-m-d h:m:s.u',$startDate);
echo $start->format('m/d/y');
重现错误的代码
推荐答案
把h
改成大的H
,因为小的是12小时制最大的是 24 小时制.
Change the h
to a big H
, since the small one is 12-hours format and the big one is 24-hours format.
您可以在手册中查看所有格式.并从那里引用:
You can see all formats in the manual. And a quote from there:
h 12 小时格式一小时,前导零从 01 到 12
H 24 小时格式,前导零从 00 到 23 的一小时
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
意味着现在您的代码失败了,因为 12 小时格式中没有 15.
Means right now your code fails, because there is no 15 in the 12 hour format.
相关文章