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

发布于 2024-12-13 15:12:49 字数 510 浏览 2 评论 0原文

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

$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));

I need to convert a given GMT date and time (YYYY-MM-DD HH:MM) into such a string YYYYMMDD representing Eastern coast date. Do you think the code below is OK?

$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));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖阳 2024-12-20 15:12:49

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

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

I'd advise against trying to do the calculation yourself. There's too many nuances with our wonderful, ever-changing, time standard. Instead, rely on PHP to perform the calculations by using php.net/date_default_timezone_set to set your timezone to GMT, then strtotime() (or the DateTime class) to get the unix-timestamp value.

Once you have the unix timestamp, use php.net/date_default_timezone_set again to set the timezone to America/New_York and use date()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文