2017-08-05 41 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,所以我可以將它們放在一個數組中,例如澳大利亞/達爾文=>澳大利亞中部標準時間 澳大利亞/墨爾本=>澳大利亞東部標準時間 澳大利亞/悉尼=>澳大利亞東部標準時間 亞洲/喀布爾=>阿富汗標準時間 美國/安克拉治=>阿拉斯加標準時間' –