Page 1 of 1

Best way to set the timezone

Posted: Tue May 26, 2009 6:46 am
by Mr Tech
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!

Re: Best way to set the timezone

Posted: Tue May 26, 2009 11:34 am
by pickle
It's best to store your timestamps as GMT - that way they'll always be the same. You can then modify those dates when displaying to the user, by using their timezone choice. Using a built in PHP function to set the timezone is probably better than setting an environment variable.

Re: Best way to set the timezone

Posted: Tue May 26, 2009 2:37 pm
by kaisellgren
Use the date_default_timezone_set() function, not putenv().

Re: Best way to set the timezone

Posted: Tue May 26, 2009 7:50 pm
by Mr Tech
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?

Re: Best way to set the timezone

Posted: Wed May 27, 2009 9:42 am
by pickle
How do you currently know what timezone your dates are in?