October 25, 2020


Thinking Magento

Daylight Savings and Magento - date_default_timezone_set problem

A strange situation I've noticed can happen if you directly use date_default_timezone_set anywhere in your PHP code.

I had this set in an Observer.php file that was activated by cron.php / cron.sh.

Once this script was activated it changed the behaviour of the cronjob timestamps.

A job that started at 9am would finish not at 9AM, but finish at 10AM.

I then noticed that in using date_default_timezone_set in s script to look for products that were on sale, it again was changing timestamps in the database.

Finally I noticed that order date/time were sometimes in the future and sometimes not. This again came about due to date_default_timezone_set  was being used against shipping methods and when those methods would be available.

Removing date_default_timezone_set removed this timezone problems and there was no need for any work around to get Magento to work with BST.

However, you still need perform a datetime lookup in your code, which means you can do one of a few things.

Either you can use the following to check date and time for BST/GMT stores

date('Y-m-d H:i:s', Mage::getModel('core/date')->Timestamp());
date('H', Mage::getModel('core/date')->Timestamp());

Or you could use something a bit more complex, however does the job as well.

$tz = new DateTimeZone(Mage::getStoreConfig('general/locale/timezone')); 
$theTime = time();$transition = $tz->getTransitions($theTime, $theTime); 
$i = $transition[0]['offset'];
date("Y-m-d", time() + $i);