I was working on a small intranet project when first had to learn about handling timezones.
Basically what I do is store all dates/times as unix timestamps throughout my DB and these are all UTC/GMT
Then I store the user's actual timezone (not offset) eg: Europe/London in their database record
Here's the list of all supported timezones...
http://uk3.php.net/timezones
Then when a user logs on to our intranet I query the database to get their user details (incl. their timezone) and use the date_default_timezone_set() function:
Code: Select all
$user_timezone = 'America/Boise';date_default_timezone_set($user_timezone); //php5 only
This sets the user's timezone for the duration of the script. Then when our server's timezone changes and daylight savings starts/ends I don't actually have to do anything!
There are various pages in our intranet where I'll need to display a date/time relative to that user's timezone... eg: a user in Australia would want to see the modification time of a contact record in their local time, not in UTC/GMT.
If I was in a non-GMT timezone, I would want to see that someone edited a record at 04:30 my time, rather than 13:30 GMT and have to work it out for myself. In these cases I use the regular date() and mktime() functions.
However there are other places where I want an absolute date, eg all sales between a certain date range or the date of an invoice stored in our database (because all dates/times are stored in UTC in our DB).
So for those I'll use the gm functions... gmdate() and gmmktime()
http://uk.php.net/manual/en/ref.datetime.php
Using the built-in timezones and functions just seemed a much better option than dealing with timezone offsets and various daylight savings times in different places throughout the world.
Please note I am no expert... but after much reading and getting it wrong I found this to be the easiest way for managing timezones in every project I've worked on. If anyone thinks I'm giving bad advice on this one then please let me know. I always want to learn the best practice methods.
It makes me want to cry when I see timezone selection boxes with GMT+1, GMT+2 etc
Hope this helps
Cheers, B