I’m currently using an open source application: WeBid (available here)
Here is the issue:
- The user has a preferred timezone stored in DB
- The site has a default timezone stored in DB
- All db stored dates are stored at “GMT-0”
The application doesn’t calculate the DST (Daylight Savings Time) correctly as it uses the following code:
(includes/functions_global.php)
$this->ctime = time() + (($this->SETTINGS['timecorrection'] + gmdate('I')) * 3600);
$this->tdiff = ($this->SETTINGS['timecorrection'] + gmdate('I')) * 3600;
gtpotyf explains:
gmdate(‘I’) -> Returns 1 if DST is active, 0 if not active. However
since gmdate always uses GMT(+0) and that timezone has no DST it will
always return 0.using date(‘I’) instead of gmdate(‘I’) would work better, but would
still not be correct since it uses the timezone from the server and
still not the users timezone.
No final correction was adopted by WeBid’s latest version, please help me resolve this issue.
In order to sort out timezones properly you’ll need to do a number of things.
Set your server to UTC, so that PHP’s
timeanddatefunctions return a UTC timestamp. (In addition to associated functions such asstrftime.Depending on your database, you can set its timezone to UTC as well. MySQL’s documentation on the subject is here.
You’ll need to migrate the datetimes stored in there to UTC if they aren’t already. (I’m not sure what you mean by GMT-0.)
Let users choose a timezone in the format ‘Europe/London’ as supported by PHP’s DateTimeZone class.
Following some research, I put together the following array of timezones to present to users in a drop down box, as I didn’t want duplicates (Amsterdam and Brussels are in the same timezone, for example.) I wouldn’t claim it to be perfect though. Depending on your user base you may want to investigate certain timezones in more detail.
When dates come out of your database, they’ll need to be converted from UTC into the user’s timezone.
You’ll need to go through the WeBid application and modify the appropriate parts to convert properly.
So, using the above code, you’ll need a couple of functions that do something like this:
Then in your class: