Php 获取所有时区
我正在尝试获取所有时区。我所做的是:Php 获取所有时区,php,datetime,timezone,Php,Datetime,Timezone,我正在尝试获取所有时区。我所做的是: $tzlist=DateTimeZone::listIdentifiers(DateTimeZone::ALL) 它给了我这样一个数组: Array ( [0] => Africa/Abidjan [1] => Africa/Accra [2] => Africa/Addis_Ababa [3] => Africa/Algiers [4] => Africa/Asmara [5]
$tzlist=DateTimeZone::listIdentifiers(DateTimeZone::ALL)代码>
它给了我这样一个数组:
Array
(
[0] => Africa/Abidjan
[1] => Africa/Accra
[2] => Africa/Addis_Ababa
[3] => Africa/Algiers
[4] => Africa/Asmara
[5] => Africa/Bamako
[6] => Africa/Bangui
[7] => Africa/Banjul
[8] => Africa/Bissau....
我想将所有时区的时区名称更改为完整时区名称
e、 g
您可以看到示例您询问的是Microsoft Windows时区的ID()。PHP使用IANA/Olson时区。有关详细信息,请参阅
从中,您可以尝试以下操作:
<?php
$timezones = DateTimeZone::listAbbreviations();
$cities = array();
foreach( $timezones as $key => $zones )
{
foreach( $zones as $id => $zone )
{
/**
* Only get timezones explicitely not part of "Others".
* @see http://www.php.net/manual/en/timezones.others.php
*/
if ( preg_match( '/^(America|Antartica|Arctic|Asia|Atlantic|Europe|Indian|Pacific)\//', $zone['timezone_id'] ) )
$cities[$zone['timezone_id']][] = $key;
}
}
// For each city, have a comma separated list of all possible timezones for that city.
foreach( $cities as $key => $value )
$cities[$key] = join( ', ', $value);
// Only keep one city (the first and also most important) for each set of possibilities.
$cities = array_unique( $cities );
// Sort by area/city name.
ksort( $cities );
?>
希望有帮助
更新:
,您有一个到PHP的Windows ids时区:
澳大利亚中央标准时间,1,澳大利亚/达尔文
澳大利亚中部标准时间,澳大利亚/达尔文
澳大利亚东部标准时间,1,澳大利亚/悉尼
澳大利亚东部标准时间,澳大利亚/墨尔本
澳大利亚东部标准时间,澳大利亚/悉尼
更改为php数组:
$timezonesArray =
['AUS Central Standard Time','1','Australia/Darwin',
'AUS Central Standard Time','AU','Australia/Darwin',
'AUS Eastern Standard Time','1','Australia/Sydney',
'AUS Eastern Standard Time','AU','Australia/Melbourne',
'AUS Eastern Standard Time','AU','Australia/Sydney',
'Afghanistan Standard Time','1','Asia/Kabul',
'Afghanistan Standard Time','AF','Asia/Kabul',,
'Alaskan Standard Time','1','America/Anchorage',
'Alaskan Standard Time','US','America/Anchorage',
'Alaskan Standard Time','US','America/Juneau',
'Alaskan Standard Time','US','America/Nome',
'Alaskan Standard Time','US','America/Sitka',
'Alaskan Standard Time','US','America/Yakutat'];
//... the array continues
然后,您可以创建一个函数,用所需的windows id(数组位置[0])转换时区(数组位置2),并使用find或任何您想要的工具
我想这不是一个更优雅的解决方案,但它会起作用,而且很简单。您可以在数组中搜索,并将所需的翻译
从一种编码返回到另一种编码
希望它现在有帮助,快乐编码 您可以使用,例如:
产出:
Australian Central Standard Time
虽然很多人会在一年中改变夏时制。如果要检查此项并添加备用日光/夏季显示名称,可以执行以下操作:
$tz = IntlTimeZone::createTimeZone("America/Los_Angeles");
echo $tz->getDisplayName(), "\n";
if ($tz->useDaylightTime()) {
echo $tz->getDisplayName(true);
}
哪些产出:
Pacific Standard Time
Pacific Daylight Time
请评论谁对问题投了否决票,以便我可以改进萨米没有投否决票,但问题/问题是什么?@Andreas我希望所有时区的时区都像Australia/Darwin
全名都是AUS Central Standard time
。我认为否决票是因为你没有解释你的确切问题,只是“你想要什么”。你尝试了什么?在$tzlist之后你的代码是什么?你是想在数组中选择澳大利亚/达尔文
,还是想将澳大利亚/达尔文
更改为澳大利亚中心…
?试着发布你的确切问题以及你搜索/尝试的内容(以及代码!)@JP.Aulet我想现在问题已经很清楚了,但是我没有找到任何有用的代码来发布这个问题Yes@JP.Aulet,我想要windows时区的ID,这样我就可以在一个数组中获取它们,比如Australia/Darwin=>AUS Central Standard time Australia/Melbourne=>AUS Easter Standard time Australia/Sydney=>AUS Easter Standard time Asia/Kabul=>afghanistan Standard Time America/Anchorage=>Alaskan Standard Time
这就是我想要的答案:)@PradeepSingh-你确定吗?在你确认你需要windows ID时,这与这些特定于地区的显示名称(来自CLDR)不同.是哪一个???@MattJohnson是的,我想要相同的,但我不确定该命名什么,当我发现windows ID看起来和我想要的一样时,我要求了
Australian Central Standard Time
$tz = IntlTimeZone::createTimeZone("America/Los_Angeles");
echo $tz->getDisplayName(), "\n";
if ($tz->useDaylightTime()) {
echo $tz->getDisplayName(true);
}
Pacific Standard Time
Pacific Daylight Time