You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP DateTime format()异常:柏林时区不同年份偏移量不一致

Europe/Berlin时区不同年份偏移量差异的原因

这不是PHP DateTime的异常,而是柏林时区的历史规则变更导致的。PHP的DateTime依赖时区数据库(tzdata)计算时区偏移,而欧洲中部时区(含柏林)在1940-1950年代有多次调整:

  • 1945年:二战结束后,柏林所在的苏占区采用UTC+3时区(对齐莫斯科时间),因此6月9日的偏移为+03:00。
  • 1946年:时区调整回UTC+2,偏移显示为+02:00。
  • 1947年:再次临时切换到UTC+3,偏移回到+03:00。
  • 1948-1949年:又调整回UTC+2,偏移保持+02:00。
  • 1950-1953年:最终恢复到UTC+1的标准中欧时间(CET),且未启用夏令时,所以偏移为+01:00。

这些历史调整被记录在时区数据库中,PHP的DateTime对象会根据日期所在年份自动匹配对应时区规则,因此同一时区下不同年份的日期会显示不同偏移量。


测试代码验证

你使用的测试代码中,createFromFormat的!Y-m-d确保日期被解析为当天0点,后续modify('+1 years')正确滚动年份,DateTime会自动根据新年份的时区规则计算偏移,最终RFC3339格式输出会体现对应偏移:

<?php
date_default_timezone_set('Europe/Berlin');

$birth_date_string = '1944-06-09';  // YYYY-MM-DD
$birth_date = \DateTime::createFromFormat('!Y-m-d', $birth_date_string);


for($i = 1; $i < 10; $i++)
{
    print_r('________________________________________________');
    $birth_date->modify('+1 years');
    var_dump($birth_date);

    $birth_date_formatted = $birth_date->format(\DateTime::RFC3339);
    var_dump($birth_date_formatted);
}

内容的提问来源于stack exchange,提问作者Thomas Schmidt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 09:23:27