Time zones for different users

I am creating a script that allows the user to select their time zone ...

If I have to do this work, how do I store dates in a database so that each time zone reads it and shows the correct date in its time zone?

I save the date as GMT, and then when a user with the selected time zone GMT +10 looks at an element in my script, do I show this date in GMT +10 times?

Is there a better way to do this?

Examples would be great :)

+7
timezone php
source share
3 answers

Saving time zones in UTC in the database is definitely the way to go.

+4
source share

UTC - how do you do it

Get the current date from your browser (use Javascript and publish the date of the browser on the server) to determine which time zone they are in and set it as the default

+1
source share

In MYSQL, Timestamp fields are always stored inside UTC, but queries return formatted strings based on the time zone of your database server, so your db server should probably be set to UTC for simplification. Pass this stuff directly to a DateTime object, then you can use its setTimezone() to make it work in your time zone.

You can also use date_default_timezone_set () to set the timezone in php that will be used for the rest of your script.

+1
source share

All Articles