Monday, 10 September 2018

Correcting the PHP timezone

The end of daylight savings in New Zealand has been changed from Sunday March 16th (today) to Sunday April 6th. Last week I looked out how to check the daylight savings dates will be applied correctly on Linux, but now that it's actually the day itself it turns out that while the operating system is using the correct date, PHP is not due to PHP5 maintainin its own internal timezone database. This post looks at how to temporarily correct the solution until it's fixed in the distro's PHP builds.
Firstly, thanks to Neil Bertram on the NZ PHP Mailing List for pointing this out this morning.
I currently manage 5 Linux machines: 2 x CentOS 5.0 running on VPS servers; 1 x CentOS 5.0 running on a physical server; 1 x CentOS 5.1; 1 x Debian 3.0. All the CentOS machines have PHP 5.1.6 running on them and the Debian one has PHP 4.4.4. To test if the servers I manage were showing the correct date and time, I wrote a test script that does this:
echo date('Y-m-d H:i:s');
The CentOS VPS machines reported the wrong date, being an hour out; the CentOS machines running on real hardware also reported the wrong date; the Debian machine reported the correct date, but this is due to (from memory) PHP 4 using the system timezone data instead of its own internal database.

Install the PECL timezonedb module

Neil suggested installing/updating the PECL timezonedb module. This is done like so:
pecl install timezonedb
On my CentOS machines there's no command "pecl" so I did a locate for it and found the script was at "/usr/share/php/peclcmd.php" so the command for my CentOS boxes would be:
sudo php /usr/share/php/peclcmd.php install timezonedb
PECL downloaded and installed the module, and finished off with this:
Build process completed successfully
Installing '/var/tmp/pear-build-root/install-timezonedb-2007.11//usr/lib/php/modules/timezonedb.so'
install ok: channel://pecl.php.net/timezonedb-2007.11
You should add "extension=timezonedb.so" to php.ini
So the next thing to do was to open the php.ini file (at /etc/php.ini on a CentOS or Red Hat Enterprise Linux machine) and add extension=timezonedb.so to it. Then it's just a matter of running:
/etc/init.d/httpd reload
and the correct timezone will now show. I re-ran my test script and date was now showing correctly.

Edit the php.ini date.timezone setting

On one of the machines, the PECL library wasn't installed, and I didn't really have time to figure out how to install it, so there is another solution. The php.ini file contains a section like this:
[Date]
; Defines the default timezone used by the date functions
; date.timezone = 
You can manully set the date.timezone setting yourself, but need to remember to change it again when the time changes to and from daylight savings. This is not the recommended solution (install the PECL module is a better idea) but can be acceptable for a quick fix.
In my case, I needed to change the setting from the default (commented out) to Etc/GMT-13 like so:
[Date]
; Defines the default timezone used by the date functions
date.timezone = Etc/GMT-13
You can find a complete list of timezones here in the PHP manual.
After doing this and reloading the Apache web server the date and time was now showing correctly on this particular machine as well. Some time between now and when daylight savings end I need to install the PECL module on that machine as well, otherwise I need to manually change it again then.

Related posts:

0 comments:

Post a Comment