Best way to set the timezone

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Best way to set the timezone

Post 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!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Best way to set the timezone

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Best way to set the timezone

Post by kaisellgren »

Use the date_default_timezone_set() function, not putenv().
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Re: Best way to set the timezone

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Best way to set the timezone

Post by pickle »

How do you currently know what timezone your dates are in?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply