代码之家  ›  专栏  ›  技术社区  ›  Bolek Lolek

含碳时区之间的差异

  •  2
  • Bolek Lolek  · 技术社区  · 6 年前

    CEST时间=UTC时间+2小时 但我的代码只显示了1小时,我不知道为什么。。。(您可以测试它 here )

    require 'Carbon/Carbon.php';
    use Carbon\Carbon;
    
    $timestamp = '2018-04-06 14:30:00';
    $date = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp, 'UTC');
    var_dump($date);
    //{ ["date"]=> string(26) "2018-04-06 14:30:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } 
    $date->tz('CEST');
    var_dump($date);
    //{ ["date"]=> string(26) "2018-04-06 15:30:00.000000" ["timezone_type"]=> int(2) ["timezone"]=> string(4) "CEST" }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   iainn    6 年前

    问题是 CEST 在PHP中不是受支持的时区,因此可以显示 "undefined behaviour" 我怀疑这被解释为 CET ,即UTC+1。

    如果改用特定的地理标识符,例如 Europe/Berlin ,它应显示正确的结果(即UTC后2小时)。尝试:

    $date->tz('Europe/Berlin');
    

    看见 https://3v4l.org/c4lve