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 :)
Storing the timezones in UTC in the database is definitely the way to go.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With