How can I get the upcoming Daylight Saving date/time change via PHP for a specific timezone? I would like to output for example:
Upcoming clock change for Berlin will be on 29.10.2017 at 3am.
$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;
}
}
This feels kind of clunky, but it tests out OK. The one-minute intervals are needed because DST changes are not limited to hourly boundaries.
<?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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.