Carbon 的时区差异
Difference between timezones with Carbon
CEST时间=UTC时间+2小时
但是我的代码只显示了 1h 我不知道为什么...(你可以测试它 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" }
问题是 CEST
在 PHP 中不受支持的时区,因此可以显示 "undefined behaviour"。我怀疑它被解释为 CET
,即 UTC+1.
如果您改用特定的地理标识符,例如 Europe/Berlin
,它应该会显示正确的结果(即距 UTC +2 小时)。尝试:
$date->tz('Europe/Berlin');
CEST时间=UTC时间+2小时 但是我的代码只显示了 1h 我不知道为什么...(你可以测试它 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" }
问题是 CEST
在 PHP 中不受支持的时区,因此可以显示 "undefined behaviour"。我怀疑它被解释为 CET
,即 UTC+1.
如果您改用特定的地理标识符,例如 Europe/Berlin
,它应该会显示正确的结果(即距 UTC +2 小时)。尝试:
$date->tz('Europe/Berlin');