【问题标题】:Getting user selected timezone date time with only offset仅使用偏移量获取用户选择的时区日期时间
【发布时间】:2023-03-16 01:29:01
【问题描述】:

在我的网站中,每个用户在注册时都必须选择他们的时区。用户数据库表有一个名为 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);

如您所见,我需要一个以某种方式接受这两个参数并在用户所在时区为我提供日期时间的函数。但我仍然不知道哪些函数对我的情况有用。

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

非常感谢大家。

【问题讨论】:

  • 为什么不创建自己的数组来将 CodeIgniter 时区映射到 PHP 时区字符串?只有有限数量的时区...

标签: php datetime codeigniter time timezone


【解决方案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];
}

【讨论】:

    【解决方案2】:

    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);
    

    【讨论】:

    • 感谢您的回复。但是 DateTimeZone 构造函数需要一个时区字符串,这在我的代码中不可用。对于 gmt_to_local 函数,它可能很有用,但要使此函数正常工作,我将不得不使用 default_timezone_set 函数将时区临时更改为用户的时区,不幸的是,default_timezone_set 需要一个时区字符串,在我的代码中再次不可用。
    • 您是否有理由需要更改时区?您可以使用数据库中的时区字符串并将其插入 gmt_to_local ...
    猜你喜欢
    • 2018-03-15
    • 1970-01-01
    • 2021-10-03
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    相关资源
    最近更新 更多