#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    162
    Rep Power
    63

    How do YOU handle time zones and DST on your website?


    'Evening gents and gals,
    I was wondering what creative solutions some have come up with for handling time zones and DST internationally? I have a couple ideas, but all have their pitfalls:

    Option 1:
    Have user select a unix style timezone (America/Chicago, America/Anchorage etc)
    Benefits: if servers are kept up to date, this should keep track of whatever DST and timezone standard is current
    Pitfall: Huge list of time zones for users to scroll through

    Option 2:
    Have user select standard time zone name (Mountain, Eastern) and check an "Honor DST Rules" checkbox
    Benefit:Only ~28 time zones to scroll through
    Pitfall:Have to keep an up to date list of when/if DST applies for each region, and run a check/calculation for each user or each page render

    Option 3:
    Fire an ajax request off to a page that updates the offset (obtained from the browser) every time a user logs in.
    Benefit:Modern browsers should follow whatever the system time is set to, and thus should be (for the most part) current.
    Pitfall: Complete reliance on javascript is silly, and an extra request/database query

    Obviously, there are a few others out there, such as combining 1 and 2

    My best example for something that does this extremely well:
    Facebook.
    That website only announces a users birthday when it's that users birthday in that users time zone (AFAIK) - you never see birthday wishes at 10pm or 9pm the previous day. Yet, I don't remember ever entering a time zone, which makes me think it uses the third option I listed.
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    You might try #3 on top of #2 or #1, but I wouldn't do it exclusively. A user may intentionally have their clock set incorrectly, so you should give them a way to work around that (or maybe they're traveling and the time changes automatically on their phone but they don't want it to change on your website).

    #1 is probably your best list. Most people know what major cities they live near. A significant number of people have no idea what time zone they live in or whether or not daylight savings time is active.

    Even if you haven't told Facebook where you live I can almost guarantee you that they have a pretty good idea already.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    162
    Rep Power
    63
    Yeah I never thought of people not knowing their timezone, but you're right, it's probably a larger amount than most would feel comfortable admitting.

    Thanks for the suggestions, I probably will go with option 1 + 3 - just have to think of a good way to break it down. Perhaps a country selector? Maybe I could have that automagically populate based on IP.

    Dunno how I'd combat time zone updates with method 3 in the situation you suggested. Perhaps turn them off if a mobile browser is detected?

    I'll brainstorm a bit haha. Thanks
  6. #4
  7. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,131
    Rep Power
    4304
    I use option2 with this list of timezones. Users that haven't selected a timezone will see times in the default that I set.
    Code:
    $timezones = array(
      '-0100' => '(GMT -1:00 hour) Azores, Cape Verde Islands',
      '-0200' => '(GMT -2:00) Mid-Atlantic',
      '-0300' => '(GMT -3:00) Brazil, Buenos Aires, Georgetown',
      '-0330' => '(GMT -3:30) Newfoundland',
      '-0400' => '(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz',
      '-0500' => '(GMT -5:00) Eastern Time (US & Canada), Bogota, Lima',
      '-0600' => '(GMT -6:00) Central Time (US & Canada), Mexico City',
      '-0700' => '(GMT -7:00) Mountain Time (US & Canada)',
      '-0800' => '(GMT -8:00) Pacific Time (US & Canada)',
      '-0900' => '(GMT -9:00) Alaska',
      '-1000' => '(GMT -10:00) Hawaii',
      '-1100' => '(GMT -11:00) Midway Island, Samoa',
      '-1200' => '(GMT -12:00) Eniwetok, Kwajalein',
      '+0000' => '(GMT) Western Europe Time, London, Lisbon, Casablanca',
      '+0100' => '(GMT +1:00 hour) Brussels, Copenhagen, Madrid, Paris',
      '+0200' => '(GMT +2:00) Kaliningrad, South Africa',
      '+0300' => '(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg',
      '+0330' => '(GMT +3:30) Tehran',
      '+0400' => '(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi',
      '+0430' => '(GMT +4:30) Kabul',
      '+0500' => '(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent',
      '+0530' => '(GMT +5:30) Bombay, Calcutta, Madras, New Delhi, Colombo',
      '+0600' => '(GMT +6:00) Almaty, Dhaka, Bangladesh',
      '+0700' => '(GMT +7:00) Bangkok, Hanoi, Jakarta',
      '+0800' => '(GMT +8:00) Beijing, Perth, Singapore, Hong Kong',
      '+0900' => '(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk',
      '+0930' => '(GMT +9:30) Adelaide, Darwin',
      '+1000' => '(GMT +10:00) Eastern Australia, Guam, Vladivostok',
      '+1100' => '(GMT +11:00) Magadan, Solomon Islands, New Caledonia',
      '+1200' => '(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka'
    );
    Have to keep an up to date list of when/if DST applies for each region
    I don't do that though. I rely on the server to know if DST is active or not.
    run a check/calculation for each user or each page render
    Don't you have to do that anyway for any of the ways to allow users to see times in their own timezone?

    Huge list of time zones for users to scroll through
    Yeah. Displaying that large list as a single level would be a mistake, but if there was a good way for users to filter through it, then it might be best.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    162
    Rep Power
    63
    Kravvitz, that's very similar to the list I'm using now haha. Infact, I think it's exactly the same - except I use seconds for the offset.

    I don't do that though. I rely on the server to know if DST is active or not.
    How does your server know it's DST for their timezone? Do you pass a guestimated area to it? The issue being that DST begins at different times and different direction depending on which country you live.

    I thought about trying to match up a unix style time zone with the offset they provide. IE if they pick GMT -7:00, the server stores it as 'America/North Dakota/Beulah and I can just cue it off with a set_default_timezone() in php. Whereas if they pick -7:00 with no DST, I can set it to 'America/Phoenix'. This will CATO once a city/state changes their DST standards, since I have no interest in following DST standards religiously.

IMN logo majestic logo threadwatch logo seochat tools logo