Work with Magento date time

Recently, when developing Daily deal module, we had a problem with setting start time for a deal. An example of the problem is: in the select box, it showed correct time to our timezone, but it saved another value in database. This is because Magento always reset timezone to 'UTC' when it saves to database. It caused us a big problem that we need to use the time to compare to current time later, caused the module to work incorrectly.

Finally, we found solution for this case. We still save the UTC time into database. When pulling the time for processing later, we will convert it into correct timezone configured in Magento back-end. This funciton will pull the correct time stamp for your saved time:

<?php $saved_time = Mage::getModel('core/date')->timestamp($deal->getTime())?>

We can use that time to compare with current time in the same timezone:

<?php $now_time = Mage::getModel('core/date')->timestamp(time()); ?>

So, one hint for working with Magento date time that you must always make sure that you are trying to compare 2 time in the same timezone. You can break more functions about date time in the model Mage_Core_Model_Date.

One thought on “Work with Magento date time”

1 Response to Work with Magento date time

  • Robert

    That is something really nice, i was looking for some tutorials on Magento Go and Enterprize, can you please post these. Thanks.