Timezone and Daylight Savings Issues

I've looked through the other solutions on SO and none of them seem to address the timezone/dst issue in the following regard.

I am making calls to NOAA Tide Prediction API and NOAA National Weather Service API which require a time range to be passed for retrieving data. For each location in my database, I have the timezone as a UTC offset and whether daylight savings time is observed (either 1 or 0). I'm trying to format some dates (todays and tomorrow) to be what the LST (Local Standard Time) would be in it's own timezone so I can pass to these API's.

I'm having trouble figuring out how to know if a date, such as todays, is within the daylight savings time range or not.

Here is what I have so far:

// Get name of timezone for tide station
// NOTE: $locationdata->timezone is something like "-5"
$tz_name = timezone_name_from_abbr("", $locationdata->timezone * 3600, false);
$dtz = new DateTimeZone($tz_name);    

// Create time range
$start_time = new DateTime('', $dtz);
$end_time = new DateTime('', $dtz);
$end_time = $end_time->modify('+1 day');

// Modify time to match local timezone
$start_time->setTimezone($dtz);
$end_time->setTimezone($dtz);

// Adjust for daylight savings time
if( $locationdata->dst == '1' )
{
   // DST is observed in this area. 

   // ** HOW DO I KNOW IF TODAY IS CURRENTLY DST OR NOT? **  

}           

// Make call to API using modified time range
...

How can I go about doing this? Thanks.


You can use PHP's time and date functions:

$tzObj = timezone_open($tz_name);
$dateObj = date_create("07.03.2012 10:10:10", $tzObj);

$dst_active = date_format($dateObj, "I");

If DST is active on the given date, $dst_active is 1 , else 0 .

Instead of specifying a time in the call to date_create you can also pass "now" to receive the value for the current date and time.

However, like Jon mentioned, different countries within the same timezone offset may observe DST while others may not.


For each location in my database, I have the timezone as a UTC offset and whether daylight savings time is observed (either 1 or 0).

That's not enough information. There can be multiple time zones which all have the same standard offset, all observe DST, but perform DST transitions at different times. (Indeed, historically they may also start and stop observing daylight saving time for several years.)

Basically, your database should contain a time zone ID, not the offset/DST-true-or-false. (Assuming PHP uses the zoneinfo time zone database, a time zone ID is something like "Europe/London".)

EDIT: To find the offset of a given DateTime , you can call getOffset , which you can then compare with the standard time offset. But unless you have the definitive time zone ID, you will be risking getting the wrong zone.


Cillosis, I hope you are not working with Java! I am and fought with time all the time. I also work with weather data. Most of the data I use is in local standard time (ignoring daylight saving time). I also need to use times from other time zones and found that Java kept reading my computer's time zone. I also kept running into deprecated classes. I came up with a solution that works. It is a bit of a kluge, so I have it heavily documented and it only exists in one function. My solution is relative time. I have set the local time to UTC. I am subtracting the GMT offset instead of adding it. I don't really care about the actual times, all I care about is the difference between two times. It is working very well. Good luck

链接地址: http://www.djcxy.com/p/29358.html

上一篇: UTC时间,时区,夏令时和夏令时切换日期

下一篇: 时区和夏令时问题