Carbon - 日期采用 ISO8601 格式,带有偏移符号

Carbon - Dates are in ISO8601 format with offset notation

我需要使用 Php Carbon 将格式为 Y-m-d H:i:s 的用户给定日期时间转换为带有偏移符号的 ISO8601 格式。所需的时区是 Europe/Berlin.

到目前为止我尝试过的是

     $from = '2021-05-01 00:00:00';
     $dateF = Carbon::createFromDate($from)->tz('Europe/Berlin')->format('Y-m-d\Th:i:sP');

我得到的结果几乎是我想要的

     2021-05-01T02:00:00+02:00

我真正需要的是准确的h:i:s

     2021-05-01T00:00:00+02:00

有什么想法吗?

请注意,您需要 H 而不是 h 0-23 小时。 而且您只需要使用解析并在其中包含日期的时区,以使 Carbon 知道必须读取日期时间的时区:

$from = '2021-05-01 00:00:00';
echo Carbon::parse("$from Europe/Berlin")->format('Y-m-d\TH:i:sP');
如果您关心 $from 变量中的 hour/minute/second,

createFromDate 就不好,因为它 returns 只有您通过当前时间的日期。