是否有办法在php获得时区列表像窗口pc
我想php代码在下拉列表中获得时区,它们显示在这个页面https://code.google.com/p/cartotheque/wiki/TimeZoneList(城市在一个选项中有类似的时区)......不在此(https://en.wikipedia.org/wiki/List_of_time_zones_by_country)页面
更新1.0
我已经按你想要的方式调整了。
完整代码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;
}
输出将显示如下
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
etc (and so on)
)
有:timezone-identifiers-list
.
You can get timezone, but as you know PHP is server side language so it will give you server timezone, so to get timezone like window pc you need to use some client side javascript, you can use jstz plug-in and you can find it here
http://pellepim.bitbucket.org/jstz/插件集成后的示例:
var timezone = jstz.determine();
timezone.name();
它会给你答案"Asia/Kolkata"
Use可以使用如下命令获取时区列表:
<?php
$timezones = DateTimeZone::listIdentifiers(DateTimeZone::ALL);
echo '<pre>';
print_r($timezones );
echo '</pre>';
?>