SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist Mr. Tech's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    562
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best way to set the time zone

    What is the best way to set the time zone? I have a website with user accounts and I want to users to have the ability to select their timezone.

    I found this function: http://au2.php.net/date_default_timezone_set ans I also found putenv("TZ=US/Eastern"); which looks a bit dodgey to me...

    What is the normal practice for changing time zone for user accounts? And what happens when the user changes their timezone? Does that mess up the dates or will it switch over? I am currently storing dates as DATETIME in the database.

    Thanks for your help!

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Best way? All depends on your requirements I guess.

    If this is just for display, like forum post time etc, store all times in one common format then manipulate it post database extraction for display.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the easiest approach is to store all time entries in the db in UTC time.

    You can use date_default_timezone_set to then set a standard site-wide timezone (if you don't want to use UTC), then allow each user to change their timezone and store it in their session.

    Each time you access a date for display, run it through a function to check which timezone to use and add or subtract time as necessary.
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  4. #4
    SitePoint Evangelist Mr. Tech's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    562
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How would I store the date as GMT? So will the date_default_timezone_set() then convert any GMT dates pulled from the database to the correct time zones time?

  5. #5
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes you use date_default_timezone_set('UTC') then insert the current time into the database.

    date("Y-m-d H:i:s");

    To retrieve it take a look at DateTime, DateTimeZone and getOffset - basically you compare the default timezone to the user timezone, find the number of seconds difference +/- and calculate the adjusted time to display.
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •