简体   繁体   English

使用strtotime和date的数据不一致且答案错误

[英]Inconsistent data and bad answers using strtotime and date

Using PHP 5.3 and given the string Sat, 20 Oct 2012 05:00:00 GMT : 使用PHP 5.3并给定字符串Sat, 20 Oct 2012 05:00:00 GMT

print $start;
$start = strtotime($start);
var_dump($start);
$hour = date('H:i A', $start);
die($hour);

That snippet of code in my program returns this: 我程序中的那段代码返回以下内容:

Sat, 20 Oct 2012 05:00:00 GMT
int(1350709200)
00:00 AM

The var_dump is correct, but the date returns a bad answer. var_dump是正确的,但是日期返回了错误的答案。

Note, also, that changing that snippet to read: 还要注意,将代码段更改为:

print $start;
$start = strtotime($start);
die($start);
$hour = date('H:i A', $start);
die($hour);

returns: 收益:

Sat, 20 Oct 2012 05:00:00 GMT

The first die returns an empty string, a null, which I can only assume is actually a boolean FALSE. 第一个die返回一个空字符串,一个null,我只能假定它实际上是一个布尔值FALSE。

So I'm actually having two different problems, here. 所以我实际上在这里有两个不同的问题。 The same answer returns two different things depending on if I var_dump or die, and the second is that date appears to be receiving the FALSE answer, not the var_dump answer. 根据我是var_dump还是死去,相同的答案会返回两种不同的结果,第二个是该日期似乎是在接受FALSE答案,而不是var_dump答案。

Help? 救命?

AS you have said, you are setting the timezone to America/Chicago. 如您所说,您将时区设置为America / Chicago。 When you call strtotime you are explicitly specifying GMT in the string. 调用strtotime时,您在字符串中明确指定了GMT。 When you then put the timestamp into Date, it is taking the timestamp and giving you the time in America/Chicago, 5 hours before GMT, which is why it is 00:00AM. 然后,当您将时间戳记为“日期”时,它将采用时间戳记并给您在美国/芝加哥的时间,格林尼治标准时间前5小时,这就是为什么00:00 AM。

You are using 5.3. 您正在使用5.3。 I would suggest using the DateTime object instead of strtotime() and date(). 我建议使用DateTime对象代替strtotime()和date()。 Its a bit easier to work with timezones etc, and the var_dumps are useful. 使用时区等要容易一些,并且var_dumps非常有用。 Create a DateTime from user input or db value and then pass that around in your code instead of timestamps. 通过用户输入或db值创建DateTime,然后在代码中传递它而不是时间戳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM