Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if a timezone observes daylight saving at any time of the year?

In PHP, you can tell if a given date is during the Daylight Savings Time period by using something like this:

$isDST = date("I", $myDate); // 1 or 0

The problem is that this only tells you whether that one point in time is in DST. Is there a reliable way to check whether DST is in effect at any time in that timezone?


Edit to clarify:

  • Brisbane, Australia does not observe daylight savings at any time of the year. All year around, it is GMT+10.
  • Sydney, Australia does, from October to March when it changes from GMT+10 to GMT+11.

I'm wondering if there would be some existing method, or a way to implement a method which works as such:

timezoneDoesDST('Australia/Brisbane');  // false
timezoneDoesDST('Australia/Sydney');  // true
like image 648
nickf Avatar asked Oct 19 '09 01:10

nickf


People also ask

Which time zones observe daylight savings time?

As of 2022, DST is observed in most of Europe, most of North America and parts of Asia around the Northern Hemisphere summer, and in parts of South America and Oceania around the Southern Hemisphere summer. It was also formerly observed in other areas.

Do all time zones observe daylight savings?

As of August 2021, the following states and territories are not observing DST: Arizona, Hawaii, American Samoa, Guam, The Northern Mariana Islands, Puerto Rico, and the Virgin Islands.

How do you know if it's daylight savings time?

Daylight Saving Time - When do we change our clocks? Most of the United States begins Daylight Saving Time at 2:00 a.m. on the second Sunday in March and reverts to standard time on the first Sunday in November.

What time zones are not affected by daylight savings?

Today, most of Arizona (except the Navajo Nation), Hawaii, the Northern Mariana Islands, Puerto Rico, Guam, American Samoa, and the U.S. Virgin Islands don't change their clocks.


2 Answers

I've found a method which works using PHP's DateTimezone class (PHP 5.2+)

function timezoneDoesDST($tzId) {
    $tz = new DateTimeZone($tzId);
    $trans = $tz->getTransitions();
    return ((count($trans) && $trans[count($trans) - 1]['ts'] > time()));
}

or, if you're running PHP 5.3+

function timezoneDoesDST($tzId) {
    $tz = new DateTimeZone($tzId);
    return count($tz->getTransitions(time())) > 0;
}

The getTransitions() function gives you information about each time the offset changes for a timezone. This includes historical data (Brisbane had daylight savings in 1916.. who knew?), so this function checks if there's an offset change in the future or not.

like image 63
nickf Avatar answered Oct 30 '22 20:10

nickf


Actually nickf method didn't works for me so I reworked it a little ...

/**
* Finds wherever a TZ is experimenting dst or not
* @author hertzel Armengol <emudojo @ gmail.com>
* @params string TimeZone -> US/Pacific for example
*
*/
function timezoneExhibitsDST($tzId) {
    $tz = new DateTimeZone($tzId);
    $date = new DateTime("now",$tz);  
    $trans = $tz->getTransitions();
    foreach ($trans as $k => $t) 
      if ($t["ts"] > $date->format('U')) {
          return $trans[$k-1]['isdst'];    
    }
}

// Usage  

var_dump(timezoneExhibitsDST("US/Pacific")); --> prints false
var_dump(timezoneExhibitsDST("Europe/London")); --> prints false
var_dump(timezoneExhibitsDST("America/Chicago")); --> prints false

same function call will return true in 1 month (March) hope it helps

like image 43
hertzel Avatar answered Oct 30 '22 19:10

hertzel