动态生成选择的形式适用于低于3月31日但是4月1日的日期和后面的错误。你可以看到我指定的GMT完全至少在日期:3月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'));
当我在我的系统(201-Mar-2019 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偏移量。
我怀疑文档中的这个说明在起作用:
注意:
始终在非相对语句之后处理相对语句。这使得“2008年7月+1周”和“2008年7月+1周”相当。
这条规则的例外情况是:“昨天”,“午夜”,“今天”,“中午”和“明天”。请注意,“明天11:00”和“明天11:00”不同。考虑到今天“2008年7月23日”的日期,第一个产生“2008-07-24 11:00”,其中第二个产生“2008-07-24 00:00”。原因是这五个陈述直接影响当前时间。