EST/EDT到GMT的转换和日期比较


EST/EDT to GMT conversion and date comparison

我需要将给定的GMT日期和时间(YYYY-MM-DD HH:MM)转换为表示东海岸日期的YYYYMMDD字符串。你认为下面的代码可以吗?

$date='2011-11-07 04:30';
$date.='-4 hours';
$date=strftime('%Y-%m-%d %H:%M',strtotime($date));
$y=gmdate('Y');  
$date2=date('Y-m-d 02:00',strtotime($y.'-03-01 second sunday'));
$date3=date('Y-m-d 02:00',strtotime($y.'-11-01 first sunday')); 
if($date<=$date2||$date>=$date3) {      
  $date.='-1 hour';
  $date=strftime('%Y-%m-%d %H:%M',strtotime($date));
} 
$date=date('Ymd', strtotime($date));

我建议不要尝试自己进行计算。我们美妙的、不断变化的时间标准有太多的细微差别。相反,依靠PHP来执行计算,使用php.net/date_default_timezone_set将您的时区设置为GMT,然后使用strtotime()(或DateTime类)来获取unix时间戳值。

有了unix时间戳后,再次使用php.net/date_default_timezone_set将时区设置为America/New_York,并使用date()