PHP 时区列表不一致
PHP timezone list inconsistent
所以我使用 drupal 6 的 date_timezone_names 生成时区列表:
/**
* A translated array of timezone names.
* Cache the untranslated array, make the translated array a static variable.
*
* @param $required
* If not required, returned array will include a blank value.
* @return
* an array of timezone names
*/
function date_timezone_names($required = FALSE, $refresh = FALSE) {
static $zonenames;
if (empty($zonenames) || $refresh) {
$cached = cache_get('date_timezone_identifiers_list');
$zonenames = !empty($cached) ? $cached->data : array();
if ($refresh || empty($cached) || empty($zonenames)) {
$data = timezone_identifiers_list(DateTimeZone::ALL_WITH_BC);
// Use include instead of include once in case the function gets
// refreshed via devel or other API and is called more than once.
if (module_exists('date_php4')) {
include('./'. drupal_get_path('module', 'date_php4') .'/date_php4_missing_data.inc');
}
foreach ($data as $delta => $zone) {
// Because many time zones exist in PHP only for backward
// compatibility reasons and should not be used, the list is
// filtered by a regular expression.
if (preg_match('!^((Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific)/|UTC$)!', $zone)) {
// https://github.com/Br3nda/drupal-module-date/blob/master/date_api.module
$zonenames[$zone] = $zone;
}
}
// If using PHP4, filter the list down to only the timezones
// the PHP4 wrapper has data for.
if (module_exists('date_php4')) {
foreach ($missing_timezone_data as $zone) {
unset($zonenames[$zone]);
}
}
// If using Event, further filter the list down to only
// zones that exist in the event module.
if (module_exists('event') && db_table_exists('event_timezones')) {
$result = db_query("SELECT name FROM {event_timezones} ORDER BY name");
$names = array();
while ($row = db_fetch_array($result)) {
$names[] = str_replace(' ', '_', $row['name']);
}
foreach ($zonenames as $name => $zone) {
if (!in_array($name, $names)) {
unset($zonenames[$name]);
}
}
}
if (!empty($zonenames)) {
cache_set('date_timezone_identifiers_list', $zonenames);
}
}
foreach ($zonenames as $zone) {
$zonenames[$zone] = t($zone);
}
}
$none = array('' => '');
return !$required ? $none + $zonenames : $zonenames;
}
注意它最终会调用 timezone_identifiers_list(DateTimeZone::ALL_WITH_BC)
但有时 return 的列表不一致...有时 Asia/Chita (GMT+8) 等时区出现在列表中...有时,该时区不存在在列表中....还要注意 Asia/Chita 不在 date_php4_missing_data.inc
中
知道为什么会这样吗?在什么情况下 $data = timezone_identifiers_list(DateTimeZone::ALL_WITH_BC);
return 时区列表不一致?
另一个有时出现有时不出现的时区:Asia/Srednekolymsk ...请注意,这也不在 date_php4_missing_data.inc
中
Asia/Chita
和 Asia/Srednekolymsk
都在 2014f 中添加到 tzdb(announcement here). This update corresponds to PHP's timezonedb 版本 2014.6.
您可以看到 timezone_version_get()
安装的 timezonedb 的当前版本,如记录 here。
完全有可能 PHP 安装不同版本的 timezonedb,无论是由于 运行 PHP 本身的旧版本,还是手动更新了 timezonedb PECL包裹。如果您关心最新的时区,那么您应该订阅 IANA tz announcements mailing list,
并根据需要对 PHP 的 timezonedb 应用相应的更新。
所以我使用 drupal 6 的 date_timezone_names 生成时区列表:
/**
* A translated array of timezone names.
* Cache the untranslated array, make the translated array a static variable.
*
* @param $required
* If not required, returned array will include a blank value.
* @return
* an array of timezone names
*/
function date_timezone_names($required = FALSE, $refresh = FALSE) {
static $zonenames;
if (empty($zonenames) || $refresh) {
$cached = cache_get('date_timezone_identifiers_list');
$zonenames = !empty($cached) ? $cached->data : array();
if ($refresh || empty($cached) || empty($zonenames)) {
$data = timezone_identifiers_list(DateTimeZone::ALL_WITH_BC);
// Use include instead of include once in case the function gets
// refreshed via devel or other API and is called more than once.
if (module_exists('date_php4')) {
include('./'. drupal_get_path('module', 'date_php4') .'/date_php4_missing_data.inc');
}
foreach ($data as $delta => $zone) {
// Because many time zones exist in PHP only for backward
// compatibility reasons and should not be used, the list is
// filtered by a regular expression.
if (preg_match('!^((Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific)/|UTC$)!', $zone)) {
// https://github.com/Br3nda/drupal-module-date/blob/master/date_api.module
$zonenames[$zone] = $zone;
}
}
// If using PHP4, filter the list down to only the timezones
// the PHP4 wrapper has data for.
if (module_exists('date_php4')) {
foreach ($missing_timezone_data as $zone) {
unset($zonenames[$zone]);
}
}
// If using Event, further filter the list down to only
// zones that exist in the event module.
if (module_exists('event') && db_table_exists('event_timezones')) {
$result = db_query("SELECT name FROM {event_timezones} ORDER BY name");
$names = array();
while ($row = db_fetch_array($result)) {
$names[] = str_replace(' ', '_', $row['name']);
}
foreach ($zonenames as $name => $zone) {
if (!in_array($name, $names)) {
unset($zonenames[$name]);
}
}
}
if (!empty($zonenames)) {
cache_set('date_timezone_identifiers_list', $zonenames);
}
}
foreach ($zonenames as $zone) {
$zonenames[$zone] = t($zone);
}
}
$none = array('' => '');
return !$required ? $none + $zonenames : $zonenames;
}
注意它最终会调用 timezone_identifiers_list(DateTimeZone::ALL_WITH_BC)
但有时 return 的列表不一致...有时 Asia/Chita (GMT+8) 等时区出现在列表中...有时,该时区不存在在列表中....还要注意 Asia/Chita 不在 date_php4_missing_data.inc
中知道为什么会这样吗?在什么情况下 $data = timezone_identifiers_list(DateTimeZone::ALL_WITH_BC);
return 时区列表不一致?
另一个有时出现有时不出现的时区:Asia/Srednekolymsk ...请注意,这也不在 date_php4_missing_data.inc
中Asia/Chita
和 Asia/Srednekolymsk
都在 2014f 中添加到 tzdb(announcement here). This update corresponds to PHP's timezonedb 版本 2014.6.
您可以看到 timezone_version_get()
安装的 timezonedb 的当前版本,如记录 here。
完全有可能 PHP 安装不同版本的 timezonedb,无论是由于 运行 PHP 本身的旧版本,还是手动更新了 timezonedb PECL包裹。如果您关心最新的时区,那么您应该订阅 IANA tz announcements mailing list, 并根据需要对 PHP 的 timezonedb 应用相应的更新。