Syndicate

Change language


BR  CA  CN  EN  HR  IT  NL  SK  SR  



Not on SSL? First get the Lanius CMS root certificate.
Next browse securely with

Not Protected by OpenSSL

Timezones with PHP4
Export to PDF Permanent link to Timezones with PHP4 Print
Written by legolas558
Sunday, 10 June 2007

Timezones with PHP4

A short tutorial which explains how to consistently implement timezones with PHP4 while remaining forward compatible with PHP5.

Update 2007/07/12: this tutorial is a top google hit, I have made some modifications so that also non-Lanius CMS users/developers can take the best out of it.

Foreword

I have been testing and reading about PHP4 timezones but I did not find any suitable implementation which fully offered a solution. This is how I worked it out (and also reflects the implementation I used in Lanius CMS); please use the Off-topic discussion forum to comment on.

All the source code snippets mentioned in this article are released under the same license of Drake CMS, GNU/GPL.

The problem

PHP5 offers a core timezones implementation; you can set the timezone using date_default_timezone_set() (since PHP 5.1.0), but how can we achieve the same result with PHP4?

Divide et impera

Here are the sub-problems into which I split the main problem:

  1. calculate the offset for the chosen timezone
  2. fix the offset for DST (this will be possible only if safe_mode is not enabled)
  3. define two wrapper functions for strftime() and date() which use the above offset and DST information

Note: the usage of the dynamically defined wrapper functions will allow us to exploit PHP5's timezones where available.

1. Calculating the timezone offset

You will need an array containing all PHP5's timezones with relative offsets; I built this array using data contained in the TimeZone class from the PEAR::Date package , from which I stripped the deprecated timezones and added (with an offset of 0) those missing but defined in PHP5.

Also, I built an array which could tell me if a certain timezone has DST enabled in its current implementation (this information comes from the zoneinfo database).

You can see the result here on the Lanius CMS SVN repository .

2. Fixing the timezone offset for DST

We first check if the timezone has any DST setting in general (using the previously defined static array), and if it has then we set the environment TZ variable to the timezone identifier using putenv() .

This will fail on PHP4 hosts which have the useless safe_mode enabled; if the putenv() setting was successful, we register a shutdown function that will set back the TZ environment variable to its original value when the execution of the script will finish.

We also store into a boolean variable whether our putenv() call was successful or not.

See the Lanius CMS implementation of the DST offset fix (SVN repository).

3. Defining the wrapper functions

Now that we have collected the needed informations, we can implement the 2 wrapper functions that will give us correctly localized time/date strings for the selected timezone.

In the Drake CMS implementation I have called those two wrapper functions lc_strftime() and lc_date(), for each of their implementation you should:

  1. add the timezone offset to the timestamp
  2. if the TZ environment variable was correctly set with putenv(), add date('I')*60*60 to the timestamp (that will fix the DST offset)
  3. return the string result using gmstrftime/gmdate with the fixed timestamp

Final notes

Since two wrapper functions are used, you can simply define them normally (as strftime()/date()) when the host has PHP5 (that's how they are implemented in Lanius CMS).

I hope this tutorial helped, if you have suggestions or comments please use the Lanius CMS Official Forums .

That's all folks.

Last updated ( Wednesday, 09 September 2009 )
 

Hosting kindly provided by C&C Advanced Online Services.