如何通过PHP针对特定时区获取即将到来的夏令时日期/时间更改?我想输出例如:
柏林即将到来的时钟将于2017年10月29日凌晨3点结束。
答案 0 :(得分:4)
$date = new DateTime();
$tz = $date->getTimezone();
$changes = $tz->getTransitions(strtotime("yesterday"), strtotime("+1 year"));
$n = count($changes);
$tnow = time();
for($i = 0; $i < $n; $i++)
{
if($changes[$i]["ts"] > $tnow)
{
echo "Upcoming clock change for " . $tz->getName() . " will be on " . $changes[$i]["time"] . "\n";
break;
}
}
答案 1 :(得分:0)
这感觉有点笨重,但它测试得很好。需要一分钟的间隔,因为DST的变化不仅限于每小时的边界。
<?php // demo/temp_g5wx.php
/**
* https://stackoverflow.com/questions/46958720/php-get-upcoming-daylight-savings-date
*/
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
// SET OUR TIME ZONE
$zone_obj = new DateTimeZone('America/New_York');
// USE ONE-MINUTE INTERVALS TO DETECT THE CHANGE
$minute = new DateInterval('PT1M');
// FOMATTING CHARACTER FOR DAYLIGHT SAVINGS TIME
$dst = 'I';
// GET A DATETIME OBJECT TO TEST
$time_obj = new DateTime('Today', $zone_obj);
$old_dst = $time_obj->format($dst);
$new_dst = $old_dst;
// ITREATE BY MINUTES
while ($old_dst == $new_dst)
{
$time_obj->add($minute);
$new_dst = $time_obj->format($dst);
}
// SHOW THE MOMENT OF CHANGE LIKE ["date"]=> string(26) "2017-11-05 01:00:00.000000"
var_dump($time_obj);