[英]Messed up PHP date() handling - possible bug?
I am writing a project with considerable date handling in PHP. 我正在用PHP处理一个具有大量日期处理的项目。 In doing so, I wrote a piece of code that was consistently malfunctioning, making use of PHP's
date()
function. 为此,我使用PHP的
date()
函数编写了始终无法正常运行的代码。 I have isolated the code (the rest of the code in the project is irrelevant), and here it is: 我已经隔离了代码(项目中的其余代码无关),在这里是:
<?php
date_default_timezone_set("GMT");
echo date("F m, Y g:ia",strtotime("April 15, 2012 10:00am"));
//Output: April 04, 2012 10:00am
//Should be: April 15, 2012 10:00am
?>
In theory (I think), this code should calculate the timestamp of April 15, 2012, at 10:00 in the morning. 从理论上讲(我认为),此代码应计算2012年4月15日上午10:00的时间戳。 This seems to be happening correctly.
这似乎是正确的情况。 The
date()
function should then turn that back into a human-readable date in the same format that was input. 然后,
date()
函数应以与输入相同的格式将其转换为人类可读的日期。 It doesn't, though. 事实并非如此。 It outputs
April 04, 2012 10:00am
. 输出
April 04, 2012 10:00am
。 In May, it says May 5th; 5月是5月5日; in June, June 6th.
在6月6日 So I think there is some bug that makes it confuse the day of the month with the month itself.
因此,我认为存在一些错误,使它与月份本身混淆。
On the other hand, it could be some weird problem that I didn't ever consider. 另一方面,这可能是我从未考虑过的一些奇怪问题。 I'm looking for second opinions/"you're so stupid you're doing X wrong"s.
我正在寻找第二意见/“您如此愚蠢,您在做X错误”。 If it is a bug, I'll report it.
如果是错误,我将报告。 I have trouble believing it's a bug, since with these dates coming up so soon I would think that it would have been noticed by now.
我很难相信这是一个错误,因为这些日期很快出现,所以我认为它现在已经被注意到。
http://php.net/manual/en/function.date.php http://php.net/manual/en/function.date.php
No, you've just messed up your date format string - F m
means "Month (textual) Month (numeric) - it's printing 04 because that's the month number for April. 不,您刚刚弄乱了日期格式字符串
F m
意思是“月(文本)月(数字)-正在打印04,因为那是4月的月份号。
You want a j
instead to print the day of the month: 您希望用
j
代替打印月份的日期:
echo date("F j, Y g:ia",strtotime("April 15, 2012 10:00am")); // prints "April 15, 2012 10:00am" as expected.
Any time you think you've detected a bug in the tools take a VERY long look at your code! 每当您认为自己在工具中检测到错误时,请非常仔细地查看代码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.