为什么 DateTime::createFromFormat() 在我的第二个示例中失败并返回布尔值? [英] Why does DateTime::createFromFormat() fails and returns a boolean in my second example?

查看:22
本文介绍了为什么 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.

这篇关于为什么 DateTime::createFromFormat() 在我的第二个示例中失败并返回布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆