简体   繁体   English

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

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

I've an object where elements are like 我有一个像元素一样的物体

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

The time string is a 24hour format and where "+00:00" is meaningless ie hard coded for all timezone. 时间字符串为24小时格式,其中"+00:00"无意义,即在所有时区都进行了硬编码。

I've to convert it to UTC time. 我必须将其转换为UTC时间。

My code- 我的代码-

$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
)

But it should be actually 2:30 pm? 但实际上应该是下午2:30?

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

Results to 结果到

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

It's wrong again. 又错了。

Any help please? 有什么帮助吗?

I would suggest looking into a solution like NTP (network time protocol). 我建议研究类似NTP(网络时间协议)的解决方案。 It will be very accurate, as well as come with many tools for manipulation and time zone conversion. 它将非常准确,并附带许多用于操纵和时区转换的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM