我有下面的代码,结果输出了20191027。
如果我修改第二行(即将时区设置为奥克兰),它将给出结果20191028。为什么会这样呢?
date_default_timezone_set("Europe/London");
#date_default_timezone_set("Pacific/Auckland");
$date_format = 'Ymd';
$day = "Sunday 4 week ago";
$start_of_the_week = strtotime($day);
$next_day = $start_of_the_week + (60 * 60 * 24 * 1);
$next_day = date($date_format, $next_day);
echo $next_day;检查2产出:
发布于 2019-11-26 09:27:49
在Europe/London时区。
DST于2019年10月27日星期日凌晨02:00结束,当地时钟倒置1小时。
请记住,strtotime在没有DST概念的unix时间戳上运行,但是date函数在格式化时将unix时间戳调整为本地时区。所以:
$start_of_the_week = strtotime("Sunday 4 week ago"); // $start_of_the_week is some unix timestamp
echo date("Y-m-d H:i:s", $start_of_the_week); // 2019-10-27 00:00:00 Europe/London time
$next_day = $start_of_the_week + (60 * 60 * 24 * 1); // you're adding 24 hours to a unix timestamp
echo date("Y-m-d H:i:s", $next_day); // 2019-10-27 23:00:00 Europe/London time而2019-10-27 23:00:00仍然是一个星期天。解决办法是增加天数而不是小时:
$next_day = strtotime("+1 day", $start_of_the_week); // 2019-10-28 00:00:00发布于 2019-11-26 09:27:09
正如在评论中所讨论的,问题是Europe/London在4周前的那一天完成了夏令时,所以在这一天增加24小时只会让你前进23小时。您可以通过使用DateTime对象来避免这样的问题,并且只使用days:
$date_format = 'Y-m-d H:i:s';
$day = "Sunday 4 week ago";
date_default_timezone_set("Europe/London");
$date = new DateTime($day);
$date->modify('+1 day');
echo $date->format($date_format) . "\n";
date_default_timezone_set("Pacific/Auckland");
$date = new DateTime($day);
$date->modify('+1 day');
echo $date->format($date_format) . "\n";输出:
2019-10-28 00:00:00
2019-10-28 00:00:00您也可以直接将时区指定给DateTime构造函数:
$date_format = 'Y-m-d H:i:s';
$day = "Sunday 4 week ago";
$date = new DateTime($day, new DateTimeZone("Europe/London"));
$date->modify('+1 day');
echo $date->format($date_format) . "\n";
$date = new DateTime($day, new DateTimeZone("Pacific/Auckland"));
$date->modify('+1 day');
echo $date->format($date_format) . "\n";发布于 2019-11-26 08:30:02
每个时区之间都有区别。
例如
印度比美国华盛顿特区早10小时30分钟。如果重复这些时区的时间,最终会给出不同的结果。
在你的例子中,“新西兰奥克兰比英国伦敦提前13小时”,因此它提供了不同的O/P
希望这能解决你对这个问题的答案:)
https://stackoverflow.com/questions/59046569
复制相似问题