treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Detect timezone?

  • So I'm using a timestamp in my database to save the time when the user posts a update. Now I've done some tweaks and now it returns "x hours ago" and stuff like that. My problem is that it is set to be GMT even if the user browsing is in America or Australia, it might say 5 hours ago when the user posts. Now how do I change my timezone to the one of the user browsing?
  • GMT+0 that is
  • If you have registered users than you can have them set their timezone in prefs which you can then store as an offset

    If they are anonymous posting you can use javascript to grab the timezone


    var timeZone = (new Date().gettimezoneOffset()/60)*(-1);


    and then fill a hidden field with it to grab and store with the message, you can then use that offset to adjust the message times
  • That is true :) Thank you :)
  • OK, I've got timezone set up in the database and register form, the values are like "America/Los_Angeles" and stuff.

    Now, how do I plus/minus from the $timestamp (0000-00-00 00:00:00) to be the same as in their timezone?



    //some mysql stuff here to pull out information for user browsing
    $thetimezone = $row['timezone'];
    }
    date_default_timezone_set($thetimezone);


    And on the wall, it's simply a $timestamp variable pulling out the timestamp for that post?
  • @bungle - Any ideas?