将Epoch时间转换为EST时区时偏差1小时,原因何在?
问题:Epoch时间转EST时区偏差1小时的原因及解决办法
你在PHP中将Epoch时间1694194200转换为可读日期时间时,结果比预期慢了1小时:
- 预期输出:Friday September 8th at 1:30 PM
- 实际输出:Friday September 8th at 12:30 PM
原因分析
问题出在你使用的EST时区上:
EST是北美东部标准时间,固定为UTC-5,不考虑夏令时;- 而你转换的时间点(2023年9月8日)属于北美东部的夏令时时段,此时当地实际使用的是
EDT(北美东部夏令时,UTC-4),直接用EST会导致时间少算1小时。
解决办法
改用America/New_York时区,这个时区会自动识别夏令时与标准时间的切换,无需手动调整:
$aptTime = 1694194200; if(!empty($aptTime)) { $aptDisplay = new DateTime("@$aptTime"); // 使用自动适配夏令时的时区 $aptDisplay->setTimezone(new DateTimeZone('America/New_York')); $aptStr = date_format($aptDisplay, "l F jS \a\t g:i A"); $html = '<p class="feature-box-desc text-dark" id="apt"><strong>' . $aptStr . '</strong></p>'; }
修改后,转换结果会自动匹配夏令时,得到你预期的Friday September 8th at 1:30 PM。
内容的提问来源于stack exchange,提问作者DDulla
相关产品推荐
相关产品推荐

