Skip to content
WJunction - Webmaster Forum

user timezone

Status
Not open for further replies.

10 comments

well i am not using any software, i am about to make register form for user where he just input username, pass and email to register but i want to integrate the timezone function for user like one have on vbulletin register page TIMEZONE.
i want to know what method is working on backend
 
the way how to do this is not very hard.

When your php program first starts to load up it uses the default timezone set in the php.ini witch is usually en-us, the secret is to set the users timezone before any use of the date functions in php. for example

PHP:
<?php
    //Script Head
    echo date('l jS \of F Y h:i:s A');
?>

This would produce the time in accordance with en-us.

You need to detect the timezone at the head of the script so no matter where you use the use data/time functions there always exact to the user and not the default server time.

PHP:
class UserLocale
{
    var $locale;
    var $timezone;
    var $languages = array();

    public function __construct()
    {
        //en_US or whatever.
        $this->locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);

        setlocale(LC_ALL, $this->locale); //Set the locale, example below

        /*
            strftime("%A %e %B %Y", mktime(0, 0, 0, 12, 22, 1978));
            Produces: vrijdag 22 december 1978
        */
        $this->detectAvailableLanguages();
    }

    private function detectAvailableLanguages()
    {
        $langs = array();

        if (isset($this->locale))
        {
            // break up string into pieces (languages and q factors)
            preg_match_all(
                '/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
                $_SERVER['HTTP_ACCEPT_LANGUAGE'],
                $lang_parse
            );

            if (count($lang_parse[1]))
            {
                // create a list like "en" => 0.8
                $this->languages = array_combine($lang_parse[1], $lang_parse[4]);
    	
                // set default to 1 for any without q factor
                foreach ($this->languages as $lang => $val)
                {
                    if ($val === '') $this->languages[$lang] = 1;
                }

                // sort list based on value
                arsort($this->languages, SORT_NUMERIC);
           }
        }
    }
}

Had enough of coding that, but the point is that you should build a class to detect as much information about the users as possible, so you can the set your server information to be the same.

So when you insert a new row into the database you can then use time() witch will produce the time according to the best possible time-zone / locale.

you can also do the same with languages, just detect the language from the http headers and then build a parser to get the available languages.

then create a language folder called en_US and add your language data in there such as strings as such, or you can use a DB to store language strings.

then just load the appropriate language strings according to the headers and then you have a real multilingual site.

Some other good resources:

http://stackoverflow.com/questions/297542/simplest-way-to-detect-client-locale-in-php
http://www.php.net/manual/en/function.date-timezone-set.php
http://www.php.net/manual/en/locale.acceptfromhttp.php
http://stackoverflow.com/questions/...lue-from-serverhttp-accept-language-using-php
http://www.thefutureoftheweb.com/blog/use-accept-language-header
 
another way to do this is using the gears API!

here's an example!

Code:
var p = {};
function geofind(successCallback, errorCallback)
{
	try
	{
		navigator.geolocation.getCurrentPosition(successCallback, errorCallback,
		{
			maximumAge: 600000
		});
	}catch(e)
	{
		try
		{
			var geolocation = {};
			geolocation = google.gears.factory.create('beta.geolocation');
			geolocation.getCurrentPosition(successCallback, errorCallback);
		} catch(e)
		{
			try
			{
				p.coords = google.loader.ClientLocation;
				successCallback(p);
			}catch(e)
			{
				errorCallback({
					message: "No Geolocation providers found"
				});
			}
		}
	}
}

and use like so:

Code:
geofind(function(result)
	{
		//Success
		$.post('set_geo_location.php',{result},function(){
			//ask the user to reload the page as you now have a geo-location in $_SESSION
		});
	},
	function(error)
	{
		//Error
	}
);
 
In your registration form add:
PHP:
<select name="TimeZone">
              <option value="1">GMT</option>
              <option value="2">GMT+1</option>
              <option value="3">GMT+2</option>
              <option value="4">GMT+ > 9000</option>
              etc
</select>
Or do it the fancy way like lite. But since you said you wanted it to be a bit like vbulletin, I think you meant this way.
 
Yea you can ask the user if you wish.

Code:
CREATE TABLE `timezones` (
`id` tinyint(3) unsigned NOT NULL auto_increment,
`timezone_location` varchar(30) NOT NULL default '',
`gmt` varchar(11) NOT NULL default '',
`offset` tinyint(2) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=143 ;

--
-- Dumping data for table `timezones`
--

INSERT INTO `timezones` VALUES (1, 'International Date Line West', '(GMT-12:00)', -12),
(2, 'Midway Island', '(GMT-11:00)', -11),
(3, 'Samoa', '(GMT-11:00)', -11),
(4, 'Hawaii', '(GMT-10:00)', -10),
(5, 'Alaska', '(GMT-09:00)', -9),
(6, 'Pacific Time (US & Canada)', '(GMT-08:00)', -8),
(7, 'Tijuana', '(GMT-08:00)', -8),
(8, 'Arizona', '(GMT-07:00)', -7),
(9, 'Mountain Time (US & Canada)', '(GMT-07:00)', -7),
(10, 'Chihuahua', '(GMT-07:00)', -7),
(11, 'La Paz', '(GMT-07:00)', -7),
(12, 'Mazatlan', '(GMT-07:00)', -7),
(13, 'Central Time (US & Canada)', '(GMT-06:00)', -6),
(14, 'Central America', '(GMT-06:00)', -6),
(15, 'Guadalajara', '(GMT-06:00)', -6),
(16, 'Mexico City', '(GMT-06:00)', -6),
(17, 'Monterrey', '(GMT-06:00)', -6),
(18, 'Saskatchewan', '(GMT-06:00)', -6),
(19, 'Eastern Time (US & Canada)', '(GMT-05:00)', -5),
(20, 'Indiana (East)', '(GMT-05:00)', -5),
(21, 'Bogota', '(GMT-05:00)', -5),
(22, 'Lima', '(GMT-05:00)', -5),
(23, 'Quito', '(GMT-05:00)', -5),
(24, 'Atlantic Time (Canada)', '(GMT-04:00)', -4),
(25, 'Caracas', '(GMT-04:00)', -4),
(26, 'La Paz', '(GMT-04:00)', -4),
(27, 'Santiago', '(GMT-04:00)', -4),
(28, 'Newfoundland', '(GMT-03:30)', -3),
(29, 'Brasilia', '(GMT-03:00)', -3),
(30, 'Buenos Aires', '(GMT-03:00)', -3),
(31, 'Georgetown', '(GMT-03:00)', -3),
(32, 'Greenland', '(GMT-03:00)', -3),
(33, 'Mid-Atlantic', '(GMT-02:00)', -2),
(34, 'Azores', '(GMT-01:00)', -1),
(35, 'Cape Verde Is.', '(GMT-01:00)', -1),
(36, 'Casablanca', '(GMT)', 0),
(37, 'Dublin', '(GMT)', 0),
(38, 'Edinburgh', '(GMT)', 0),
(39, 'Lisbon', '(GMT)', 0),
(40, 'London', '(GMT)', 0),
(41, 'Monrovia', '(GMT)', 0),
(42, 'Amsterdam', '(GMT+01:00)', 1),
(43, 'Belgrade', '(GMT+01:00)', 1),
(44, 'Berlin', '(GMT+01:00)', 1),
(45, 'Bern', '(GMT+01:00)', 1),
(46, 'Bratislava', '(GMT+01:00)', 1),
(47, 'Brussels', '(GMT+01:00)', 1),
(48, 'Budapest', '(GMT+01:00)', 1),
(49, 'Copenhagen', '(GMT+01:00)', 1),
(50, 'Ljubljana', '(GMT+01:00)', 1),
(51, 'Madrid', '(GMT+01:00)', 1),
(52, 'Paris', '(GMT+01:00)', 1),
(53, 'Prague', '(GMT+01:00)', 1),
(54, 'Rome', '(GMT+01:00)', 1),
(55, 'Sarajevo', '(GMT+01:00)', 1),
(56, 'Skopje', '(GMT+01:00)', 1),
(57, 'Stockholm', '(GMT+01:00)', 1),
(58, 'Vienna', '(GMT+01:00)', 1),
(59, 'Warsaw', '(GMT+01:00)', 1),
(60, 'West Central Africa', '(GMT+01:00)', 1),
(61, 'Zagreb', '(GMT+01:00)', 1),
(62, 'Athens', '(GMT+02:00)', 2),
(63, 'Bucharest', '(GMT+02:00)', 2),
(64, 'Cairo', '(GMT+02:00)', 2),
(65, 'Harare', '(GMT+02:00)', 2),
(66, 'Helsinki', '(GMT+02:00)', 2),
(67, 'Istanbul', '(GMT+02:00)', 2),
(68, 'Jerusalem', '(GMT+02:00)', 2),
(69, 'Kyev', '(GMT+02:00)', 2),
(70, 'Minsk', '(GMT+02:00)', 2),
(71, 'Pretoria', '(GMT+02:00)', 2),
(72, 'Riga', '(GMT+02:00)', 2),
(73, 'Sofia', '(GMT+02:00)', 2),
(74, 'Tallinn', '(GMT+02:00)', 2),
(75, 'Vilnius', '(GMT+02:00)', 2),
(76, 'Baghdad', '(GMT+03:00)', 3),
(77, 'Kuwait', '(GMT+03:00)', 3),
(78, 'Moscow', '(GMT+03:00)', 3),
(79, 'Nairobi', '(GMT+03:00)', 3),
(80, 'Riyadh', '(GMT+03:00)', 3),
(81, 'St. Petersburg', '(GMT+03:00)', 3),
(82, 'Volgograd', '(GMT+03:00)', 3),
(83, 'Tehran', '(GMT+03:30)', 3),
(84, 'Abu Dhabi', '(GMT+04:00)', 4),
(85, 'Baku', '(GMT+04:00)', 4),
(86, 'Muscat', '(GMT+04:00)', 4),
(87, 'Tbilisi', '(GMT+04:00)', 4),
(88, 'Yerevan', '(GMT+04:00)', 4),
(89, 'Kabul', '(GMT+04:30)', 4),
(90, 'Ekaterinburg', '(GMT+05:00)', 5),
(91, 'Islamabad', '(GMT+05:00)', 5),
(92, 'Karachi', '(GMT+05:00)', 5),
(93, 'Tashkent', '(GMT+05:00)', 5),
(94, 'Chennai', '(GMT+05:30)', 5),
(95, 'Kolkata', '(GMT+05:30)', 5),
(96, 'Mumbai', '(GMT+05:30)', 5),
(97, 'New Delhi', '(GMT+05:30)', 5),
(98, 'Kathmandu', '(GMT+05:45)', 5),
(99, 'Almaty', '(GMT+06:00)', 6),
(100, 'Astana', '(GMT+06:00)', 6),
(101, 'Dhaka', '(GMT+06:00)', 6),
(102, 'Novosibirsk', '(GMT+06:00)', 6),
(103, 'Sri Jayawardenepura', '(GMT+06:00)', 6),
(104, 'Rangoon', '(GMT+06:30)', 6),
(105, 'Bangkok', '(GMT+07:00)', 7),
(106, 'Hanoi', '(GMT+07:00)', 7),
(107, 'Jakarta', '(GMT+07:00)', 7),
(108, 'Krasnoyarsk', '(GMT+07:00)', 7),
(109, 'Beijing', '(GMT+08:00)', 8),
(110, 'Chongqing', '(GMT+08:00)', 8),
(111, 'Hong Kong', '(GMT+08:00)', 8),
(112, 'Irkutsk', '(GMT+08:00)', 8),
(113, 'Kuala Lumpur', '(GMT+08:00)', 8),
(114, 'Perth', '(GMT+08:00)', 8),
(115, 'Singapore', '(GMT+08:00)', 8),
(116, 'Taipei', '(GMT+08:00)', 8),
(117, 'Ulaan Bataar', '(GMT+08:00)', 8),
(118, 'Urumqi', '(GMT+08:00)', 8),
(119, 'Osaka', '(GMT+09:00)', 9),
(120, 'Sapporo', '(GMT+09:00)', 9),
(121, 'Seoul', '(GMT+09:00)', 9),
(122, 'Tokyo', '(GMT+09:00)', 9),
(123, 'Yakutsk', '(GMT+09:00)', 9),
(124, 'Adelaide', '(GMT+09:30)', 9),
(125, 'Darwin', '(GMT+09:30)', 9),
(126, 'Brisbane', '(GMT+10:00)', 10),
(127, 'Canberra', '(GMT+10:00)', 10),
(128, 'Guam', '(GMT+10:00)', 10),
(129, 'Hobart', '(GMT+10:00)', 10),
(130, 'Melbourne', '(GMT+10:00)', 10),
(131, 'Port Moresby', '(GMT+10:00)', 10),
(132, 'Sydney', '(GMT+10:00)', 10),
(133, 'Vladivostok', '(GMT+10:00)', 10),
(134, 'Magadan', '(GMT+11:00)', 11),
(135, 'New Caledonia', '(GMT+11:00)', 11),
(136, 'Solomon Is.', '(GMT+11:00)', 11),
(137, 'Auckland', '(GMT+12:00)', 12),
(138, 'Fiji', '(GMT+12:00)', 12),
(139, 'Kamchatka', '(GMT+12:00)', 12),
(140, 'Marshall Is.', '(GMT+12:00)', 12),
(141, 'Wellington', '(GMT+12:00)', 12),
(142, 'Nuku''alofa', '(GMT+13:00)', 13);

then just loop the results with mysql / php and set the column offset to the rel tag in the select loop and use JavaScript to try auto select.

PHP:
    var gmtHours = new Date().getTimezoneOffset() / 60;

    //jQuery
    $('#lselect[rel*='+gmtHours +']').attr('selected','selected');
 
Status
Not open for further replies.

About the author

W
Active Member · Joined
704
Messages
23
Reactions
18
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom