Etc/GMT+(偏移量)仅在PHP中提供GMT


Etc/GMT+(offset) only providing GMT in PHP

根据PHP,格式为Etc/GMT+(offset)的时区是有效的时区。基于此,我假设此代码将输出一系列不同的时间:

$start_date = 'Dec 16, 2011';
$start_time = '09:45:00';
#$utime_zone = 'GMT'; <-- switching to GMT works!
$utime_zone = 'Etc/GMT';
for($i = -12; $i <= 12; $i++){
   $curr = ($i >= 0)?'+'.$i: $i;
   $str = ($start_date . " " . $start_time . " " . $utime_zone.$curr);
   echo $str . ' = ' . strtotime($str).PHP_EOL;
}

不幸的是,strtotime调用都完全忽略了时区。

输出:
Dec 16, 2011 09:45:00 Etc/GMT-12 = 1324028700
Dec 16, 2011 09:45:00 Etc/GMT-11 = 1324028700
Dec 16, 2011 09:45:00 Etc/GMT-10 = 1324028700
...you get the idea.

很明显,我对此做了一些假设,但有没有一种简单的方法可以将Etc/GMT+(offset)转换为GMT时间戳?

我不知道为什么你的案例不起作用(除了PHP警告不要使用那种方法之外),但你可以使用ISO-8601日期格式轻松解决它

格式基本上是Y-m-d'TH:i:s,然后是UTC的Z+/-hh:mm

$datetime = '2011-06-20T18:49:00';
echo $datetime.'+01:00 => '.strtotime($datetime.'+01:00')."<br />";
echo $datetime.'+03:00 => '.strtotime($datetime.'+03:00')."<br />";

给你

2011-06-20T18:49:00+01:00 => 1308592140
2011-06-20T18:49:00+03:00 => 1308584940