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

查看:182
本文介绍了为什么DateTime :: createFromFormat()失败,并返回一个布尔值在我的第二个例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行这个第一个正确的创建到一个日期。第二个失败,返回一个布尔值,所以我不能格式化。是时间超出范围?

  //正常工作
$ 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');

//不能正常工作
$ 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小时的格式。 p>

您可以在手册中查看所有格式。并从那里引用:


h 12小时格式,前导零01到12 <
H 24小时格式与前导零00到23之间的一小时

意思是现在你的代码失败了,因为12小时格式中没有15个。


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');

Code to reproduce the error

解决方案

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-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23

Means right now your code fails, because there is no 15 in the 12 hour format.

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

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