[英]php date formatting not working
$t = DateTime::createFromFormat('Gi', '900');
$time_str = $t->format('gi a');
echo $time_str; //outputs 600 pm instead of 9am. Why? and How do I get 9am?
I am not sure where I am going wrong.. I am following what is given here in terms of date formatting: http://php.net/manual/en/function.date.php 我不知道我哪里出错了。我按照这里给出的日期格式: http : //php.net/manual/en/function.date.php
Thanks! 谢谢!
The documentation you linked is for the date()
function. 您链接的文档是date()
函数。 The DateTime::createFromFormat
is not the same (though the format strings are pretty much identical). DateTime::createFromFormat
不一样(尽管格式字符串非常相似)。
I expect the format parsing is having trouble recognizing the difference between the hour and minute components. 我希望格式解析无法识别小时和分钟组件之间的差异。
If you split them up with a space, you get the desired result: 如果你用空格拆分它们,你会得到所需的结果:
$t = DateTime::createFromFormat('G i', '9 00');
$time_str = $t->format('gi a');
echo $time_str;
// Output is 900 am
Edit: 编辑:
The inability for PHP to parse a format string like Gi
is a known bug . PHP无法解析像Gi
这样的格式字符串是一个已知的错误 。 The parser for G
doesn't know whether to read 9
or 90
and in the latter case that 90
is too high. G
的解析器不知道是读取9
还是90
,而后者则不知道90
是否过高。
As i said in my comment this works : 正如我在评论中所说,这有效:
$t = DateTime::createFromFormat('G i', '9 00');
$time_str = $t->format('gi a');
echo $time_str.PHP_EOL;
Cannot find any where written down - but suspect the time needs to be separated ... either by space or colon or something else 找不到任何写下来的地方 - 但怀疑时间需要分开......无论是空格还是冒号或其他东西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.