douyangqian5243 2012-03-07 17:52
浏览 190
已采纳

时区和夏令时问题

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.

  • 写回答

3条回答

  • duanji9677 2012-03-07 18:20
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料