timezone_abbreviations_list()
Come usare timezone_abbreviations_list() e DateTimeZone::listAbbreviations() in PHP per recuperare le abbreviazioni dei fusi orari e i loro identificatori IANA.
Introduzione
Un'abbreviazione di fuso orario è un codice breve di 2-5 lettere come EST, GMT o CEST che identifica lo scostamento di una regione dall'Ora Universale Coordinata (UTC). PHP è in grado di elencare tutte le abbreviazioni che conosce, insieme agli identificatori di fuso orario IANA (come America/New_York) a cui ciascuna corrisponde.
Questo capitolo spiega come recuperare tale elenco, perché le abbreviazioni da sole non sono affidabili, e fornisce una tabella di riferimento dei codici più comuni.
timezone_abbreviations_list() è stata deprecata in PHP 8.1 e rimossa in PHP 8.2. Su PHP 8.2 e versioni successive, usa l'equivalente DateTimeZone::listAbbreviations(). Entrambe restituiscono la stessa struttura dati.
Perché le abbreviazioni sono ambigue
A differenza degli identificatori IANA, le abbreviazioni non sono univoche. CST da solo può significare Central Standard Time (Nord America, UTC-6), China Standard Time (UTC+8) o Cuba Standard Time. BST può essere British Summer Time oppure Bangladesh Standard Time. Poiché lo stesso codice può corrispondere a più regioni, non dovresti mai memorizzare o configurare un fuso orario tramite la sua abbreviazione. Risolvi sempre l'identificatore IANA (ad esempio Europe/London) e passalo a date_default_timezone_set() o a un oggetto DateTimeZone.
È esattamente a questo che serve l'elenco delle abbreviazioni: convertire un codice ricevuto (ad esempio dall'input dell'utente) negli identificatori candidati a cui potrebbe corrispondere — vedi timezone_name_from_abbr() per selezionare una singola corrispondenza.
Elencare tutte le abbreviazioni
Entrambe le funzioni restituiscono un array associativo. Ogni chiave è un'abbreviazione in minuscolo; ogni valore è un array di zone, dove ogni zona ha tre campi:
dst—truese la voce è una variante dell'ora legale.offset— lo scostamento UTC in secondi.timezone_id— l'identificatore IANA (può esserenullper gli scostamenti senza zona denominata).
<?php
// PHP 8.2+ (use timezone_abbreviations_list() on older versions)
$abbreviations = DateTimeZone::listAbbreviations();
print_r($abbreviations['acst']);Output:
Array
(
[0] => Array
(
[dst] =>
[offset] => 34200
[timezone_id] => Australia/Adelaide
)
...
)Lo scostamento 34200 secondi corrisponde a 34200 / 3600 = 9.5 ore, ovvero UTC+9:30.
Mappare le abbreviazioni agli identificatori e agli scostamenti
Per trasformare la struttura grezza in righe leggibili, itera l'array esterno e poi ogni zona:
<?php
$abbreviations = DateTimeZone::listAbbreviations();
foreach ($abbreviations as $abbr => $zones) {
foreach ($zones as $zone) {
$hours = $zone['offset'] / 3600;
printf(
"%-5s => %-25s (UTC %+.1f)\n",
strtoupper($abbr),
$zone['timezone_id'] ?? '(unnamed)',
$hours
);
}
}Alcune righe di esempio dell'output:
GMT => Europe/London (UTC +0.0)
GMT => Africa/Abidjan (UTC +0.0)
CET => Europe/Paris (UTC +1.0)L'elenco è molto esteso (migliaia di righe), quindi in pratica lo si filtra sui codici di interesse anziché stamparlo per intero.
Abbreviazioni comuni dei fusi orari
Di seguito è riportato un elenco di riferimento delle abbreviazioni di fuso orario più comuni (le chiavi dell'array sono in minuscolo in PHP; qui mostrate in maiuscolo per leggibilità):
- ACDT - Australian Central Daylight Time
- ACST - Australian Central Standard Time
- ACT - Acre Time
- ADT - Atlantic Daylight Time
- AEDT - Australian Eastern Daylight Time
- AEST - Australian Eastern Standard Time
- AFT - Afghanistan Time
- AKDT - Alaska Daylight Time
- AKST - Alaska Standard Time
- AMST - Amazon Summer Time
- AMT - Amazon Time
- ART - Argentina Time
- AST - Atlantic Standard Time
- AWST - Australian Western Standard Time
- AZOST - Azores Standard Time
- AZT - Azerbaijan Time
- BDT - Brunei Time
- BNT - Brunei Darussalam Time
- BOT - Bolivia Time
- BRST - Brasilia Summer Time
- BRT - Brasilia Time
- BST - British Summer Time
- BTT - Bhutan Time
- CAT - Central Africa Time
- CCT - Cocos Islands Time
- CDT - Central Daylight Time
- CEST - Central European Summer Time
- CET - Central European Time
- CHADT - Chatham Island Daylight Time
- CHAST - Chatham Island Standard Time
- CHOT - Choibalsan Standard Time
- CHST - Chamorro Standard Time
- CHT - Chuuk Time
- CKT - Cook Island Time
- CLST - Chile Summer Time
- CLT - Chile Standard Time
- COT - Colombia Time
- CST - Central Standard Time
- CT - Central Time
- CVT - Cape Verde Time
- CWST - Central Western Standard Time
- CXT - Christmas Island Time
- DAVT - Davis Time
- DDUT - Dumont-d'Urville Time
- EASST - Easter Island Summer Time
- EAST - Easter Island Standard Time
- EAT - Eastern Africa Time
- ECT - Eastern Caribbean Time
- EDT - Eastern Daylight Time
- EEST - Eastern European Summer Time
- EET - Eastern European Time
- EGST - Eastern Greenland Summer Time
- EGT - Eastern Greenland Time
- EIT - Eastern Indonesian Time
- EST - Eastern Standard Time
- FET - Further Eastern European Time
- FJT - Fiji Time
- FKST - Falkland Islands Summer Time
- FKT - Falkland Islands Time
- FNT - Fernando de Noronha Time
- GALT - Galapagos Time
- GAMT - Gambier Islands Time
- GET - Georgia Standard Time
- GFT - French Guiana Time
- GILT - Gilbert Island Time
- GIT - Gambier Island Time
- GMT - Greenwich Mean Time
- GST - Gulf Standard Time
- GYT - Guyana Time
- HADT - Hawaii-Aleutian Daylight Time
- HAST - Hawaii-Aleutian Standard Time
- HKT - Hong Kong Time
- HST - Hawaii Standard Time
- ICT - Indochina Time
- IDT - Israel Daylight Time
- IRDT - Iran Daylight Time
- IRST - Iran Standard Time
- IST - India / Irish / Israel Standard Time (ambiguous)
- JST - Japan Standard Time
- KST - Korea Standard Time
- MDT - Mountain Daylight Time
- MSK - Moscow Standard Time
- MST - Mountain Standard Time
- NZDT - New Zealand Daylight Time
- NZST - New Zealand Standard Time
- PDT - Pacific Daylight Time
- PHT - Philippine Time
- PKT - Pakistan Standard Time
- PST - Pacific Standard Time
- SGT - Singapore Time
- UTC - Coordinated Universal Time
- WAT - West Africa Time
- WEST - Western European Summer Time
- WET - Western European Time
- WIB - Western Indonesian Time
- WITA - Central Indonesian Time
L'elenco precedente è un sottoinsieme comune. DateTimeZone::listAbbreviations() restituisce l'array completo di tutte le abbreviazioni registrate, che dovresti iterare in modo programmatico (come mostrato sopra) anziché affidarti a un elenco gestito manualmente.
Funzioni correlate
- PHP Timezones — panoramica di
DateTimeZonee degli identificatori IANA. timezone_identifiers_list()— elenca tutti gli identificatori IANA supportati da PHP.timezone_name_from_abbr()— risolve una singola abbreviazione in un identificatore.date_default_timezone_set()— imposta il fuso orario predefinito per lo script.