如何在 PHP 中从本地时间(没有日期)获取 UTC 时间


How to get UTC time from local time (without date) in PHP?

我有一个对象,其中元素类似于

i) "StartTime":"0001-01-01T09:30:00+00:00""Timezone":"NEW YORK"

时间字符串是 24 小时格式,其中 "+00:00" 是无意义的,即对所有时区进行硬编码。

我必须将其转换为 UTC 时间。

我的代码-

$startTime = new 'DateTime(substr($obj->Session->StartTime, 0, -6));
$startTime->setTimezone(new 'DateTimeZone('UTC'));
print_r($startTime);
DateTime Object
(
[date] => 0001-01-01 03:36:40
[timezone_type] => 3
[timezone] => UTC
)

但实际上应该是下午 2:30?

ii) "StartTime":"0001-01-01T08:00:00+00:00""Timezone":"LONDON"

结果到

DateTime Object
(
[date] => 0001-01-01 02:06:40
[timezone_type] => 3
[timezone] => UTC
)

又错了。

请帮忙吗?

我建议研究像NTP(网络时间协议)这样的解决方案。它将非常准确,并带有许多用于操作和时区转换的工具。