从时间格式yyyy-mm-ddThh:mm:ss.s+zzzzzz获取正确的时区名称


Get correct timezone name from time format yyyy-mm-ddThh:mm:ss.s+zzzzzz

我正在使用一个以iso标准(yyyy-mm-ddThh:mm:ss.s+zzzzzz)返回时间的api地点:

yyyy is the year
mm (first) is the month
dd is the day
T is a separator indicating that time-of-day follows
hh is the hour on a 24-hour clock
mm (second) is the minute
ss is whole seconds
s (optional) is the fractional second
zzzzzz is the time zone

如2015 - 07 - 31 - t13:00:00.000 + 03:00

我想从+03:00

获得正确的时区

我在stackoverflow上尝试过的其他解决方案:

$date = new DateTime('2015-07-31T13:00:00.000+03:00');
$newtz = date_timezone_get($date);
$offset= timezone_name_get($newtz);
$timezone = preg_replace('/[^0-9]/', '', $offset) * 36;
$timezone_name = timezone_name_from_abbr('', $offset, true);
echo $timezone_name;

上面的代码输出:Europe/Helsinki,但我期待Africa/Nairobi

问题是我如何使代码输出Africa/Nairobi ?

还有,如果存在两个时区具有相同的偏移量,例如:Europe/MinskAfrica/Nairobi的偏移量为+03:00

正如其他人所注意到的,这是一对多的关系(比如1:50左右)。可能有一种稍微干净一点的方法,但这是您需要的:

$date = new DateTime('2015-07-31T13:00:00.000+03:00');
$newtz = date_timezone_get($date);
$offset_string= timezone_name_get($newtz);
$offset = preg_replace('/[^0-9]/', '', $offset_string) * 36;
$full_list_of_timezones = timezone_abbreviations_list();
foreach ($full_list_of_timezones as $array1) {
    foreach ($array1 as $array2) {
        if ($array2[offset] == $offset) {
            echo $array2[timezone_id]."<br>'n";
        }
    }
}