获取用户选择的时区日期时间仅包含偏移量

时间:2011-02-16 23:00:28

标签: php datetime codeigniter time timezone

在我的网站中,每个用户在注册时都必须选择他们的时区。用户数据库表有一个名为timezone的列,其值是以下数组中的键之一:

$zones = array(
'UM12'      => -12,
'UM11'      => -11,
'UM10'      => -10,
'UM95'      => -9.5,
'UM9'       => -9,
'UM8'       => -8,
'UM7'       => -7,
'UM6'       => -6,
'UM5'       => -5,
'UM45'      => -4.5,
'UM4'       => -4,
'UM35'      => -3.5,
'UM3'       => -3,
'UM2'       => -2,
'UM1'       => -1,
'UTC'       => 0,
'UP1'       => +1,
'UP2'       => +2,
'UP3'       => +3,
'UP35'      => +3.5,
'UP4'       => +4,
'UP45'      => +4.5,
'UP5'       => +5,
'UP55'      => +5.5,
'UP575'     => +5.75,
'UP6'       => +6,
'UP65'      => +6.5,
'UP7'       => +7,
'UP8'       => +8,
'UP875'     => +8.75,
'UP9'       => +9,
'UP95'      => +9.5,
'UP10'      => +10,
'UP105'     => +10.5,
'UP11'      => +11,
'UP115'     => +11.5,
'UP12'      => +12,
'UP1275'    => +12.75,
'UP13'      => +13,
'UP14'      => +14
);

此外,Codeigniter为我提供了以下数组,但这没有太大帮助,因为它的值不能直接用于在PHP中创建DateTimeZone对象。

$lang['UM12']   = '(UTC -12:00) Baker/Howland Island';
$lang['UM11']   = '(UTC -11:00) Samoa Time Zone, Niue';
$lang['UM10']   = '(UTC -10:00) Hawaii-Aleutian Standard Time, Cook Islands, Tahiti';
$lang['UM95']   = '(UTC -9:30) Marquesas Islands';
$lang['UM9']    = '(UTC -9:00) Alaska Standard Time, Gambier Islands';
$lang['UM8']    = '(UTC -8:00) Pacific Standard Time, Clipperton Island';
$lang['UM7']    = '(UTC -7:00) Mountain Standard Time';
$lang['UM6']    = '(UTC -6:00) Central Standard Time';
$lang['UM5']    = '(UTC -5:00) Eastern Standard Time, Western Caribbean Standard Time';
$lang['UM45']   = '(UTC -4:30) Venezuelan Standard Time';
$lang['UM4']    = '(UTC -4:00) Atlantic Standard Time, Eastern Caribbean Standard Time';
$lang['UM35']   = '(UTC -3:30) Newfoundland Standard Time';
$lang['UM3']    = '(UTC -3:00) Argentina, Brazil, French Guiana, Uruguay';
$lang['UM2']    = '(UTC -2:00) South Georgia/South Sandwich Islands';
$lang['UM1']    = '(UTC -1:00) Azores, Cape Verde Islands';
$lang['UTC']    = '(UTC) Greenwich Mean Time, Western European Time';
$lang['UP1']    = '(UTC +1:00) Central European Time, West Africa Time';
$lang['UP2']    = '(UTC +2:00) Central Africa Time, Eastern European Time, Kaliningrad Time';
$lang['UP3']    = '(UTC +3:00) Moscow Time, East Africa Time';
$lang['UP35']   = '(UTC +3:30) Iran Standard Time';
$lang['UP4']    = '(UTC +4:00) Azerbaijan Standard Time, Samara Time';
$lang['UP45']   = '(UTC +4:30) Afghanistan';
$lang['UP5']    = '(UTC +5:00) Pakistan Standard Time, Yekaterinburg Time';
$lang['UP55']   = '(UTC +5:30) Indian Standard Time, Sri Lanka Time';
$lang['UP575']  = '(UTC +5:45) Nepal Time';
$lang['UP6']    = '(UTC +6:00) Bangladesh Standard Time, Bhutan Time, Omsk Time';
$lang['UP65']   = '(UTC +6:30) Cocos Islands, Myanmar';
$lang['UP7']    = '(UTC +7:00) Krasnoyarsk Time, Cambodia, Laos, Thailand, Vietnam';
$lang['UP8']    = '(UTC +8:00) Australian Western Standard Time, Beijing Time, Irkutsk Time';
$lang['UP875']  = '(UTC +8:45) Australian Central Western Standard Time';
$lang['UP9']    = '(UTC +9:00) Japan Standard Time, Korea Standard Time, Yakutsk Time';
$lang['UP95']   = '(UTC +9:30) Australian Central Standard Time';
$lang['UP10']   = '(UTC +10:00) Australian Eastern Standard Time, Vladivostok Time';
$lang['UP105']  = '(UTC +10:30) Lord Howe Island';
$lang['UP11']   = '(UTC +11:00) Magadan Time, Solomon Islands, Vanuatu';
$lang['UP115']  = '(UTC +11:30) Norfolk Island';
$lang['UP12']   = '(UTC +12:00) Fiji, Gilbert Islands, Kamchatka Time, New Zealand Standard Time';
$lang['UP1275'] = '(UTC +12:45) Chatham Islands Standard Time';
$lang['UP13']   = '(UTC +13:00) Phoenix Islands Time, Tonga';
$lang['UP14']   = '(UTC +14:00) Line Islands';

目前,Web服务器的default_timezone设置为UTC,数据库中所有日期时间值都采用mysql日期时间格式存储在此时区中。

因此,要根据用户的时区正确显示日期时间,我需要一种方法来进行转换。这是我到目前为止所得到的:

$timezone_offset=$zones[$user_row->timezone];
$utc_timestamp=strtotime($user_row->registered_on);

正如您所看到的,我需要一个以某种方式接受这两个参数的函数,并在用户的时区中给出日期时间。但我仍然无法弄清楚哪些功能对我的情况有用。

有人会介意给我一些建议吗?

非常感谢大家。

2 个答案:

答案 0 :(得分:1)

http://www.php.net/manual/en/datetime.settimezone.php

<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";

$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>

同样适用于Codeigniter: http://codeigniter.com/user_guide/helpers/date_helper.html

gmt_to_local()

  

采用Unix时间戳(引用   GMT)作为输入,并将其转换为   基于的本地化时间戳   时区和夏令时   提交。示例:$ timestamp =   '1140153693'; $ timezone ='UM8';   $ daylight_saving = TRUE;

echo gmt_to_local($timestamp, $timezone, $daylight_saving);

答案 1 :(得分:1)

我所做的是创建一个新的timezones_string助手

function timezones_string($tz = '')
{
    // Note: Don't change the order of these even though
    // some items appear to be in the wrong order

    $zones = array(
                    'UM12'      => '-12:00',
                    'UM11'      => '-11:00',
                    'UM10'      => '-10:00',
                    'UM95'      => '-9:30',
                    'UM9'       => '-9:00',
                    'UM8'       => '-8:00',
                    'UM7'       => '-7:00',
                    'UM6'       => '-6:00',
                    'UM5'       => '-5:00',
                    'UM45'      => '-4:30',
                    'UM4'       => '-4:00',
                    'UM35'      => '-3:30',
                    'UM3'       => '-3:00',
                    'UM2'       => '-2:00',
                    'UM1'       => '-1:00',
                    'UTC'       => '+0:00',
                    'UP1'       => '+1:00',
                    'UP2'       => '+2:00',
                    'UP3'       => '+3:00',
                    'UP35'      => '+3:30',
                    'UP4'       => '+4:00',
                    'UP45'      => '+4:30',
                    'UP5'       => '+5:00',
                    'UP55'      => '+5:30',
                    'UP575'     => '+5:45',
                    'UP6'       => '+6:00',
                    'UP65'      => '+6:30',
                    'UP7'       => '+7:00',
                    'UP8'       => '+8:00',
                    'UP875'     => '+8:45',
                    'UP9'       => '+9:00',
                    'UP95'      => '+9:30',
                    'UP10'      => '+10:00',
                    'UP105'     => '+10:30',
                    'UP11'      => '+11:00',
                    'UP115'     => '+11:30',
                    'UP12'      => '+12:00',
                    'UP1275'    => '+12:45',
                    'UP13'      => '+13:00',
                    'UP14'      => '+14:00',
                );

    if ($tz == '')
    {
        return $zones;
    }

    if ($tz == 'GMT')
        $tz = 'UTC';

    return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
}