EST/EDT 到 GMT 的转换和日期比较
我需要将给定的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议不要尝试自己进行计算。我们美妙的、不断变化的时间标准有太多的细微差别。相反,依靠 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 theDateTime
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()