note 119178 added to timezones
| From: | wimarbueno at gmail dot com | Date: | Sat, 16 Apr 2016 00:33:27 +0000 |
| Subject: | note 119178 added to timezones | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205240@lists.php.net to get a copy of this message | ||
Se genera la lista de zonas horarias de forma dinámica, y por lo tanto se actualiza
automáticamente cuando se podrÃan cambiar las zonas horarias (en PHP).
function timezone_list() {
static $timezones = null;
if ($timezones === null) {
$timezones = [];
$offsets = [];
$now = new DateTime();
foreach (DateTimeZone::listIdentifiers() as $timezone) {
$now->setTimezone(new DateTimeZone($timezone));
$offsets[] = $offset = $now->getOffset();
$timezones[$timezone] = '(' . format_GMT_offset($offset) . ') ' .
format_timezone_name($timezone);
}
array_multisort($offsets, $timezones);
}
return $timezones;
}
function format_GMT_offset($offset) {
$hours = intval($offset / 3600);
$minutes = abs(intval($offset % 3600 / 60));
return 'GMT' . ($offset ? sprintf('%+03d:%02d', $hours, $minutes) :
'');
}
function format_timezone_name($name) {
$name = str_replace('/', ', ', $name);
$name = str_replace('_', ' ', $name);
$name = str_replace('St ', 'St. ', $name);
return $name;
}
// Uso
$timezone = timezone_list();
echo '<pre>';
print_r($timezone);
echo '</pre>';
// Aquà es un ejemplo de la salida:
Array
(
[Pacific/Midway] => (GMT-11:00) Pacific, Midway
[Pacific/Niue] => (GMT-11:00) Pacific, Niue
[Pacific/Pago_Pago] => (GMT-11:00) Pacific, Pago Pago
[America/Adak] => (GMT-10:00) America, Adak
[Pacific/Honolulu] => (GMT-10:00) Pacific, Honolulu
[Pacific/Johnston] => (GMT-10:00) Pacific, Johnston
[Pacific/Rarotonga] => (GMT-10:00) Pacific, Rarotonga
[Pacific/Tahiti] => (GMT-10:00) Pacific, Tahiti
[Pacific/Marquesas] => (GMT-09:30) Pacific, Marquesas
[America/Anchorage] => (GMT-09:00) America, Anchorage
[America/Juneau] => (GMT-08:00) America, Juneau
[America/Metlakatla] => (GMT-08:00) America, Metlakatla
[America/Nome] => (GMT-08:00) America, Nome
[America/Sitka] => (GMT-08:00) America, Sitka
[America/Yakutat] => (GMT-08:00) America, Yakutat
[Pacific/Pitcairn] => (GMT-08:00) Pacific, Pitcairn
[America/Creston] => (GMT-07:00) America, Creston
[America/Dawson] => (GMT-07:00) America, Dawson
...
)
----
Server IP: 72.52.91.14
Probable Submitter: 2001:1388:1b8a:70a1:817f:bf49:e283:5bd5
----
Manual Page -- http://php.net/manual/en/timezones.php
Edit -- https://master.php.net/note/edit/119178
Del: integrated -- https://master.php.net/note/delete/119178/integrated
Del: useless -- https://master.php.net/note/delete/119178/useless
Del: bad code -- https://master.php.net/note/delete/119178/bad+code
Del: spam -- https://master.php.net/note/delete/119178/spam
Del: non-english -- https://master.php.net/note/delete/119178/non-english
Del: in docs -- https://master.php.net/note/delete/119178/in+docs
Del: other reasons-- https://master.php.net/note/delete/119178
Reject -- https://master.php.net/note/reject/119178
Search -- https://master.php.net/manage/user-notes.php