Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time Zones for different users

Tags:

timezone

php

I am creating a script that allows the user to choose their own timezone...

If I am to make this work, how do I store dates in the database so that every timezone will read it, and show the correct date in their timezone?

Do I store the date as GMT and then when a user with the timezone GMT +10 selected views the item within my script, I show that date in GMT +10 time?

Is there a better way to do this?

Examples would be great :)

like image 582
Ben Sinclair Avatar asked Mar 24 '10 23:03

Ben Sinclair


3 Answers

Storing the timezones in UTC in the database is definitely the way to go.

like image 82
Mark Byers Avatar answered Oct 18 '22 13:10

Mark Byers


UTC is how you do it

Get a current date from their browser (use Javascript and post the browsers date to the server) to work out what time zone they are in and set it as a default

like image 40
TFD Avatar answered Oct 18 '22 13:10

TFD


In MYSQL, Timestamp fields are always saved internally in UTC, but queries will return formatted strings based on the timezone of your database server, so your db server should probably be set to UTC to simplify things. Pass that stuff straight into a DateTime object, then you can use it's setTimezone() function to make it operate in the timezone you want.

You can also use date_default_timezone_set() to set a timezone in php, which will be used for the remainder of your script.

like image 1
keithjgrant Avatar answered Oct 18 '22 15:10

keithjgrant