Netshine Software Limited







Home arrow Developer Blog Make Text BiggerMake Text SmallerReset Text Size

Nov 04
2008

PHP date function displays dates a day early after the clocks changed

Posted by netshine in timezonephp.iniPHPdateclocksBritish Summer Time

I was surprised to find that after the clocks changed on 26th October 2008, all of the dates on my billing records were showing up a day early. Invoices paid on 28th September showed as being paid on 27th. I tried it out on 3 different servers as well as my local PC. On the 3 Linux servers, it always calculated the wrong date, but on my PC (running Windows XP), it showed the correct date. The explanation was fairly simple, but I found it infuriatingly difficult to fix!

All the dates are stored in the database as a UNIX timestamp (number of seconds since 1st January 1970), and the 'time' part of it is always set to zero (ie. midnight). When the clocks went back, PHP started interpreting the dates an hour earlier - so midnight becomes 11pm on the previous day - hence, all the records showing the wrong date. But why was PHP apparently ignoring the fact that dates before 26th October were recorded during British Summer Time (BST)?

I googled until I was blue in the face, but could find no mention of a bug in PHP to this effect. Eventually I figured out that if PHP is behaving as if there is no such thing as daylight saving time, it must be unaware of where in the world it is running. After rummaging round the PHP documentation, I discovered that all I needed to do was tell PHP which timezone I am in. So I added this to my php.ini file:

date.timezone = Europe/London

...restarted Apache, and hey presto! Dates are being displayed correctly again. Cool

This is why PHP5 reports a 'strict' error saying that it is not safe to rely on the system's timezone settings!



Comments (0)Add Comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

security code
Write the displayed characters


busy
Joomla! Tutorial
Take a look at our free tutorial for Joomla! Administrator...


Login Form





Lost Password?

© 2004-2010 Netshine Software Limited   Terms and Conditions