Hosting News & Commentary Hosting News & Commentary Hosting News & Commentary

No I wasn’t late for a meeting due to differing timezones, but it is official I hate dealing with timezones - infact it was from the first day I had to do any programming related to timezones that I hated working with them.

A lot of these problems stem from building websites (more specifically our Breeze CMS) that need to cater for visitors that can visit from different timezones and need to see data being shown in their current time. You can also face issues if your hosting provider is located outside of your timezone. But not to worry - we have come up with some solutions to these problems as I have spent forever trying to get my head around the issues we have faced.

Unix Timestamp

Here at PX we prefer to work with Unix Timestamps as PHP has an obvious foundation built on these with the many standard date functions requiring a timestamp for processing. The Unix Timestamp is based on the number of seconds since Epoch Time which is - January 1st 1970 GMT. What this means is if you output the timestamp via php (”echo time();”) no matter what timezone your server is in the timestamp will be the same figure - provided the server is properly synced for time!

PHP Date Functions and Timestamps

One thing that can be easily missed or not obvious about the PHP date functions (such as date(); and strtotime();) is that they will output the date for the timestamp given based on the current timezone for the server. So if you were to do the following code:

echo

You get the following results:
1210341600 - Australia (EST)
1210402800 - USA (California)

(For those in Britain/Australia please take note that in strtotime(); you can use a ‘-’ instead of a ‘/’ for dates and it will be interpreted as a UK format date instead of US format)

Thats a 17 hour difference! This is because Australia is +10 GMT whereas California is -7 GMT. You can easily start to see where you can have problems if your hosting changes to a different timezone!

Our Solution

Our solution to this problem is to first of all store the timestamp as is from the server as it is (using time();) the same no matter where you are and therefore universal.

It is on its output that you need to alter it to suit the person visiting the website. In Breeze we have two timezone settings - one for when not logged in (default) and another which the user can choose for themself. We also have two Daylight Savings (DST) settings as well which work the same as the timezone.

When outputting a date/time on the site from a timestamp we use our own userdate(); function instead of the date(); function - it has the same inputs but it outputs the correct date/time for the current visitor:

function userdate($format, $time=”) {if (!$time) {$time = time();}$gmt = $time - date(’Z');$user = $gmt + user_gmt_diff();return date($format, $user);}// Example usuageecho userdate(’Y-m-d H:i’); // returns 2008-05-08 10:05

You will notice you need another function that does the user_gmt_diff(); - below is a sample on how we do it in our own system but you can alter to suit how you store the timezone for the site/visitor:

function user_gmt_diff() {$user = $User->details;if (!$user) {$user[’TZ’] = = $setting->DST;}$diff = timezones($user[’TZ’]) * 3600;if ($user[’DST’]){$diff += 3600;}return Solution (when using mySQL datetime fields)

If you are using mySQL date/time fields then you need something to make your dates ‘universal’ - we suggest to store your date/time fields as the GMT time when it happened. Below is a simple way to get the correct GMT datetime for right now to be inserted into the database:

$gmt_time = time()-date(’Z');$gmt_mysql = date(’Y-m-d H:i:s’,$gmt_time);echo $gmt_mysql;

  1. No user reviews yet.


Leave a Reply