2017-08-05 129 views
-2

我正在尝试获取所有时区。我所做的是:获取所有时区

$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.... 

我想要的时区名称更改为全时区名称所有时区 例如

Australia/Darwin = AUS Central Standard Time 

你可以看到的例子here

+0

请评论谁是downvoting的问题,所以我可以提高相同 –

+1

我没有倒下,但是问题是什么? – Andreas

+0

@Andreas我想要所有时区的“澳大利亚/达尔文”全名为“AUS中央标准时间”的时区 –

回答

2

您可以使用IntlTimeZone::getDisplayName(),例如,:

echo IntlTimeZone::createTimeZone("Australia/Darwin")->getDisplayName(); 

输出:

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 
+0

那就是我正在寻找的答案:) –

+1

@PradeepSingh - 你确定吗?在[此评论](https://stackoverflow.com/questions/45521276/get-all-time-ones#comment78003673_45521624)你确认你想要Windows ID。这与这些特定于语言环境的显示名称(来自CLDR)不同。这是什么? –

+0

@MattJohnson是的,我想要的是相同的,但我没有证实什么名称,当我发现Windows Ids看起来与我想要的一样,我要求 –

0

你所要求的Microsoft Windows时区(here)的标识。 PHP使用IANA/Olson时区。详情请参阅timezone tag wiki

PHP official documentation,你可以试试这个:

<?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); 

?> 

这里另一个SO问题,你可以看看,有很多类似的答案:Generating a drop down list of timezones with PHP

如果没有这种帮助,你可以试试以上代码,将convert数据添加到您的时区。例如,让我们假设我们有一个UTC日期和时间字符串(2017-08-05 02:45),我们希望将其转换为ACST(澳大利亚中部标准时间)。

<?php 
$utc_date = DateTime::createFromFormat(
    'Y-m-d G:i', 
    '2017-08-05 02:45', 
    new DateTimeZone('UTC') 
); 

$acst_date = clone $utc_date; // we don't want PHP's default pass object by reference here 
$acst_date->setTimeZone(new DateTimeZone('Australia/Yancowinna')); 

echo 'UTC: ' . $utc_date->format('Y-m-d g:i A'); // UTC: 2017-08-05 02:45 AM 
echo 'ACST: ' . $acst_date->format('Y-m-d g:i A'); // ACST: 2017-08-05 14:15 PM 

希望它有帮助!

UPDATE:

From here,你有一个Windows IDS时区到PHP:

澳大利亚中部标准时间1,澳大利亚/达尔文 澳大利亚中部标准时间,AU,澳大利亚/达尔文 AUS东部标准时间1,澳大利亚/悉尼 澳大利亚东部标准时间,AU,澳大利亚/墨尔本 澳大利亚东部标准时间,AU,澳大利亚/悉尼

变化的pH P排列的:

$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 

,然后你可以做一个功能,你的时区(阵列位置2)与所需的窗口ID(阵列位置[0]),与find或任何你想要的翻译。

这不是我猜想的更优雅的解决方案,但它会工作而且简单。您可以搜索数组并将所需的translation从一个编码返回到另一个编码。

希望它现在有帮助,快乐的编码!

+0

是@JP .Aulet,我需要窗口时区的ID,所以我可以将它们放在一个数组中,例如澳大利亚/达尔文=>澳大利亚中部标准时间 澳大利亚/墨尔本=>澳大利亚东部标准时间 澳大利亚/悉尼=>澳大利亚东部标准时间 亚洲/喀布尔=>阿富汗标准时间 美国/安克拉治=>阿拉斯加标准时间' –