2011-03-19 9 views
11

A partire da un identificatore di fuso orario, ad esempio "America/Los_Angeles", come si trovano i nomi e le abbreviazioni di quel fuso orario in PHP? Per esempio:Come ottenere i nomi e le abbreviazioni di un fuso orario in PHP?

'PST', 'Pacific Standard Time', 'PDT', 'Pacific Daylight Time' 

Se potessi ottenere solo la breve sigla ('PST' e 'PDT'), che sarebbe stato ok.

Ho guardato a DateTimeZone::listAbbreviations() e ho provato a controllare che per vedere quale corrispondenza con il mio ID, tuttavia per America/Los_Angeles, trova "PST", "PDT", "PPT" e "PWT" che è leggermente curioso .

+0

Secondo https://github.com/eggert/tz/blob/master/northamerica # L146, sembra che "W" e "P" in PWT e PPT stanno rispettivamente per "guerra" e "pace". – gvl

risposta

7

speranza che questo vi aiuterà

<?php 
    date_default_timezone_set('Europe/Sofia'); 
    echo date_default_timezone_get(); // Europe/Sofia 
    echo ' => '.date('T'); // => EET 
?> 
+2

Grazie, ma questo va solo a metà. Ho già l'id ("Europa/Sofia"), e sto cercando il nome del fuso orario stesso ("Eastern European Time"/"Eastern European Summer Time") – nickf

1

Sembra Symphony ha metodi per questo, ad esempio, select_timezone_tag. Potresti controllare their source code per vedere come è fatto.

9

Spero che questo aiuta:

function get_timezone_abbreviation($timezone_id) 
{ 
    if($timezone_id){ 
     $abb_list = timezone_abbreviations_list(); 

     $abb_array = array(); 
     foreach ($abb_list as $abb_key => $abb_val) { 
      foreach ($abb_val as $key => $value) { 
       $value['abb'] = $abb_key; 
       array_push($abb_array, $value); 
      } 
     } 

     foreach ($abb_array as $key => $value) { 
      if($value['timezone_id'] == $timezone_id){ 
       return strtoupper($value['abb']); 
      } 
     } 
    } 
    return FALSE; 
} 

get_timezone_abbreviation ('America/New_York');

e si ottiene:

EDT

+0

Non funziona correttamente per 'echo get_timezone_abbreviation ('Asia/Kolkata'); ' –

+0

@DebeshNayak Quale versione di PHP stai usando? –

+0

@DebeshNayak, ho appena testato diverse versioni di PHP e funziona. Si prega di dare un'occhiata: http://take.ms/Pp0Ws –

1

La cosa è, un nome di fuso orario dipende dal periodo dell'anno, ad esempio, in inverno è CET, in estate è CEST.

Possiamo ottenere il nome del fuso orario utilizzando la data e l'ora correnti.

$timezone = 'Pacific/Midway'; 
$dt = new DateTime('now', new DateTimeZone($timezone)); 
$abbreviation = $dt->format('T'); // SST 

supporta solo i fusi orari che php conosce, non sapeva cosa fosse "Pacific Standard Time".

Qui si può vedere come si passa tra il CET e CEST

$t = new CDateTime('2015-09-22 11:00', new DateTimeZone('CET')); 
    $t->format('T'); // CEST 

    $t = new CDateTime('2015-12-22 11:00', new DateTimeZone('CET')); 
    $t->format('T'); // CET 
5

Spero che questo vi aiuterà a

<?php 
$dateTime = new DateTime(); 
$dateTime->setTimeZone(new DateTimeZone('America/Havana')); 
echo $dateTime->format('T'); 
?> 
Problemi correlati