bobo
bobo

Reputation: 8727

Getting user selected timezone date time with only offset

In my website, every user has to select their timezone when they register . The user database table has a column named timezone and its value is one of the keys in the following array:

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

Also, Codeigniter has the following array for me, but this does not help too much because its values cannot be used directly to create a DateTimeZone object in PHP.

$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';

Currently, the web server has default_timezone set to UTC and all date time values in the database which are in mysql datetime format are stored in this timezone.

So, for a date time to display correctly according to the timezone of a user, I need a way to do the conversion. This is what I have come up so far:

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

As you can see, I need a function that somehow accepts these two parameters and give me the date time in the timezone of the user. But I still can't figure out which functions can be useful for my case.

Would anyone mind giving me some suggestions?

Many thanks to you all.

Upvotes: 4

Views: 7134

Answers (2)

red
red

Reputation: 23

What I did is the creation of a new timezones_string helper

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];
}

Upvotes: 1

Kieran Andrews
Kieran Andrews

Reputation: 5885

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";
?>

Also for Codeigniter: http://codeigniter.com/user_guide/helpers/date_helper.html

gmt_to_local()

Takes a Unix timestamp (referenced to GMT) as input, and converts it to a localized timestamp based on the timezone and Daylight Saving time submitted. Example: $timestamp = '1140153693'; $timezone = 'UM8'; $daylight_saving = TRUE;

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

Upvotes: 1

Related Questions