[英]Converting “2013-09-06 10:30pm GMT+1” string to PHP DateTIme object in UTC
I am trying to convert the following string: 2013-09-06 10:30pm GMT+1
to a PHP DateTime
object. 我正在尝试将以下字符串转换为:
2013-09-06 10:30pm GMT+1
到PHP DateTime
对象。 In the process, it also has to convert it to UTC. 在此过程中,还必须将其转换为UTC。 I tried the code below but obviously that was incorrect, but I don't really know where to go from here:
我尝试了下面的代码,但显然那是不正确的,但我真的不知道从这里去哪里:
$datetime = DateTime::createFromFormat('YY-MM-DD HH-IIm tzcorrection', '2013-09-06 10:30pm GMT+1', new DateTimeZone('UTC'));
I know there are lot of similar topics out there. 我知道那里有很多类似的话题。 But they do not cover this format and the conversion to UTC.
但是它们不涵盖此格式以及转换为UTC。 Because of the conversion to UTC, I believe it is not possible to use
strtotime()
for this. 由于转换为UTC,因此我相信无法使用
strtotime()
。
This will work just fine:- 这样就可以了:
$date = new \DateTime('2013-09-06 10:30pm GMT+1');
$date->setTimezone(new \DateTimeZone('UTC'));
var_dump($date);
See it working ; 看到它起作用 ;
See also http://php.net/date for more information on date string formats. 另请参见http://php.net/date以获取有关日期字符串格式的更多信息。
One issue with your date string is the 'GMT+1' part, there is no valid date string for this (hopefully somebody else can help with that), so it is difficult to make \\DateTime::createFromFormat() work in this case. 日期字符串的一个问题是'GMT + 1'部分,没有有效的日期字符串(希望有人可以提供帮助),因此在这种情况下很难使\\ DateTime :: createFromFormat()工作。 However the DateTime constructor seems to cope with it quite well when left to its own devices.
但是,DateTime构造函数放在自己的设备上似乎可以很好地应对它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.