我正在使用一个以iso标准返回时间的api(yyyy-mm-ddThh:mm:ss.s+zzzzzz
)
其中:
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-31T13: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/Minsk
和Africa/Nairobi
的偏移量为+03:00
答案 0 :(得分:0)
正如其他人所说,它是一对多关系(如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";
}
}
}