如何将 UTC 日期时间转换为 BST 时间英国夏令时 (BST) Laravel Carbon

How to Convert a UTC datetime to BST Time British Summer Time (BST) Laravel Carbon

我正在尝试将 UTC 时间转换为 BST。如果我是正确的,它会显示错误的结果。

$timestamp =  '2020-03-30 16:34:00';
$date = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $timestamp, 'Europe/London');
$date->tz('BST');

$bst =  $date->toDateTimeString();

以上代码输出 2020-03-30 15:34:00。请注意,BST 在输出中少了一小时。

PHP 似乎不支持 BST。它不在 Europe or other timezones. I'm not sure why it doesn't throw an exception when you use BST, but the docs 支持的时区列表中,在这里说:

The behavior of timezones not listed here is undefined.

很明显 "BST" 无法正常工作,因为当您使用 BST 时区创建时间时,输出总是显示 +00:00,即使在夏季也是如此:

>>> $date = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-30 16:34:00', 'BST');
=> Carbon\Carbon @1580402040 {#3261
     date: 2020-01-30 16:34:00.0 +00:00,
   }
>>> $date = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', '2020-05-30 16:34:00', 'BST');
=> Carbon\Carbon @1590856440 {#3258
     date: 2020-05-30 16:34:00.0 +00:00,
   }

但是,当您使用 Europe/London 时区时,输出会在冬季显示 +00:00(本质上是 'UTC'),而 +01:00(本质上是 'BST') 在夏季:

>>> $date = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', '2020-01-30 16:34:00', 'Europe/London');
=> Carbon\Carbon @1580402040 {#3256
     date: 2020-01-30 16:34:00.0 Europe/London (+00:00),
   }
>>> $date = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', '2020-05-30 16:34:00', 'Europe/London');
=> Carbon\Carbon @1590852840 {#3251
     date: 2020-05-30 16:34:00.0 Europe/London (+01:00),
   }

所以基于位置的时区已经考虑了夏令时。