奇怪的 strtotime 与格林威治标准时间一直完美运行到 3 月 31 日
weird strtotime with GMT which worked perfectly till march 31
动态生成 select 格式,该格式适用于 3 月 31 日之前的日期,但对于 4 月 1 日及之后的日期是错误的。您可以看到我专门指定了 GMT,这至少对 date:March 31.
非常有效
$today = strtotime("today GMT");
<select name="date">
<option value=<?php echo $d = strtotime('0 day',$today); ?>> <?php echo date('d M, Y', $d).'-'.$d; ?></option>
<option value=<?php echo $d = strtotime('1 day',$today); ?>> <?php echo date('d M, Y', $d).'-'.$d; ?></option>
<option value=<?php echo $d = strtotime('2 day',$today); ?>> <?php echo date('d M, Y', $d).'-'.$d; ?></option>
<-remainings->
</select>
生成代码
28 Mar, 2019-1553731200<--Correct March 28, 2019 12:00:00 AM
29 Mar, 2019-1553817600<--Correct
30 Mar, 2019-1553904000<--Correct
31 Mar, 2019-1553990400<--Correct
01 Apr, 2019-1554073200<--Wrong March 31, 2019 11:00:00 PM (this and remainings should be April <nextday>, 2019 12:00:00 AM)
02 Apr, 2019-1554159600<--Wrong April 1, 2019 11:00:00 PM
03 Apr, 2019-1554246000<--Wrong April 2, 2019 11:00:00 PM
04 Apr, 2019-1554332400<--Wrong April 3, 2019 11:00:00 PM
05 Apr, 2019-1554418800<--Wrong April 4, 2019 11:00:00 PM
06 Apr, 2019-1554505200<--Wrong April 5, 2019 11:00:00 PM
尝试将此 $today = strtotime("today GMT");
更改为此
$today = strtotime("today",gmdate('U'));
当我在我的系统(2019 年 3 月 31 日 18:00 时区 EDT)上执行这个 $today = strtotime("today GMT");
时,结果是 1553990400 30 Mar, 2019 20:00
我阅读了 PHP: Datetime Relative Formats Doc,但没有发现任何格式使用时区的迹象,这就是我尝试使用 gmdate('U')
.
的原因
此代码:
echo "\ngmdate\n";
echo "current date: ",strtotime("today"),"<-- ",date('d M, Y H:i'),"\n";
echo "'today GMT': ",strtotime("today GMT"),"<--",date('d M, Y H:i',strtotime("today GMT")),"\n\n";
$todayGMdate = strtotime("today",gmdate('U'));
echo $todayGMdate,"<-- ",date('d M, Y H:i',$todayGMdate),"\n";
for ($i = 0; $i < 10; $i++) {
$d=strtotime("+$i day",$todayGMdate);
echo date('d M, Y', $d).'-'.$d," <-- ",date('d M, Y H:i',$d),"\n";
}
产生这个结果:
gmdate
current date: 1554091200<-- 01 Apr, 2019 10:44
'today GMT': 1554076800<--31 Mar, 2019 20:00
1554091200<-- 01 Apr, 2019 00:00
01 Apr, 2019-1554091200 <-- 01 Apr, 2019 00:00
02 Apr, 2019-1554177600 <-- 02 Apr, 2019 00:00
03 Apr, 2019-1554264000 <-- 03 Apr, 2019 00:00
04 Apr, 2019-1554350400 <-- 04 Apr, 2019 00:00
05 Apr, 2019-1554436800 <-- 05 Apr, 2019 00:00
06 Apr, 2019-1554523200 <-- 06 Apr, 2019 00:00
07 Apr, 2019-1554609600 <-- 07 Apr, 2019 00:00
08 Apr, 2019-1554696000 <-- 08 Apr, 2019 00:00
09 Apr, 2019-1554782400 <-- 09 Apr, 2019 00:00
10 Apr, 2019-1554868800 <-- 10 Apr, 2019 00:00
似乎 strtotime("today GMT")
在当前语言环境中获取今天的开始,然后添加 gmt 偏移量。
我怀疑文档中的这条注释在起作用:
注:
Relative statements are always processed after non-relative
statements. This makes "+1 week july 2008" and "july 2008 +1 week"
equivalent.
Exceptions to this rule are: "yesterday", "midnight", "today", "noon"
and "tomorrow". Note that "tomorrow 11:00" and "11:00 tomorrow" are
different. Considering today's date of "July 23rd, 2008" the first one
produces "2008-07-24 11:00" where as the second one produces
"2008-07-24 00:00". The reason for this is that those five statements
directly influence the current time.
动态生成 select 格式,该格式适用于 3 月 31 日之前的日期,但对于 4 月 1 日及之后的日期是错误的。您可以看到我专门指定了 GMT,这至少对 date:March 31.
非常有效 $today = strtotime("today GMT");
<select name="date">
<option value=<?php echo $d = strtotime('0 day',$today); ?>> <?php echo date('d M, Y', $d).'-'.$d; ?></option>
<option value=<?php echo $d = strtotime('1 day',$today); ?>> <?php echo date('d M, Y', $d).'-'.$d; ?></option>
<option value=<?php echo $d = strtotime('2 day',$today); ?>> <?php echo date('d M, Y', $d).'-'.$d; ?></option>
<-remainings->
</select>
生成代码
28 Mar, 2019-1553731200<--Correct March 28, 2019 12:00:00 AM
29 Mar, 2019-1553817600<--Correct
30 Mar, 2019-1553904000<--Correct
31 Mar, 2019-1553990400<--Correct
01 Apr, 2019-1554073200<--Wrong March 31, 2019 11:00:00 PM (this and remainings should be April <nextday>, 2019 12:00:00 AM)
02 Apr, 2019-1554159600<--Wrong April 1, 2019 11:00:00 PM
03 Apr, 2019-1554246000<--Wrong April 2, 2019 11:00:00 PM
04 Apr, 2019-1554332400<--Wrong April 3, 2019 11:00:00 PM
05 Apr, 2019-1554418800<--Wrong April 4, 2019 11:00:00 PM
06 Apr, 2019-1554505200<--Wrong April 5, 2019 11:00:00 PM
尝试将此 $today = strtotime("today GMT");
更改为此
$today = strtotime("today",gmdate('U'));
当我在我的系统(2019 年 3 月 31 日 18:00 时区 EDT)上执行这个 $today = strtotime("today GMT");
时,结果是 1553990400 30 Mar, 2019 20:00
我阅读了 PHP: Datetime Relative Formats Doc,但没有发现任何格式使用时区的迹象,这就是我尝试使用 gmdate('U')
.
此代码:
echo "\ngmdate\n";
echo "current date: ",strtotime("today"),"<-- ",date('d M, Y H:i'),"\n";
echo "'today GMT': ",strtotime("today GMT"),"<--",date('d M, Y H:i',strtotime("today GMT")),"\n\n";
$todayGMdate = strtotime("today",gmdate('U'));
echo $todayGMdate,"<-- ",date('d M, Y H:i',$todayGMdate),"\n";
for ($i = 0; $i < 10; $i++) {
$d=strtotime("+$i day",$todayGMdate);
echo date('d M, Y', $d).'-'.$d," <-- ",date('d M, Y H:i',$d),"\n";
}
产生这个结果:
gmdate
current date: 1554091200<-- 01 Apr, 2019 10:44
'today GMT': 1554076800<--31 Mar, 2019 20:00
1554091200<-- 01 Apr, 2019 00:00
01 Apr, 2019-1554091200 <-- 01 Apr, 2019 00:00
02 Apr, 2019-1554177600 <-- 02 Apr, 2019 00:00
03 Apr, 2019-1554264000 <-- 03 Apr, 2019 00:00
04 Apr, 2019-1554350400 <-- 04 Apr, 2019 00:00
05 Apr, 2019-1554436800 <-- 05 Apr, 2019 00:00
06 Apr, 2019-1554523200 <-- 06 Apr, 2019 00:00
07 Apr, 2019-1554609600 <-- 07 Apr, 2019 00:00
08 Apr, 2019-1554696000 <-- 08 Apr, 2019 00:00
09 Apr, 2019-1554782400 <-- 09 Apr, 2019 00:00
10 Apr, 2019-1554868800 <-- 10 Apr, 2019 00:00
似乎 strtotime("today GMT")
在当前语言环境中获取今天的开始,然后添加 gmt 偏移量。
我怀疑文档中的这条注释在起作用:
注:
Relative statements are always processed after non-relative statements. This makes "+1 week july 2008" and "july 2008 +1 week" equivalent.
Exceptions to this rule are: "yesterday", "midnight", "today", "noon" and "tomorrow". Note that "tomorrow 11:00" and "11:00 tomorrow" are different. Considering today's date of "July 23rd, 2008" the first one produces "2008-07-24 11:00" where as the second one produces "2008-07-24 00:00". The reason for this is that those five statements directly influence the current time.