Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento ouputting incorrect date and time

If I output the date/time in index.php echo date('m/d/Y h:i:s a', time());. It's correct. If I output the date/time anywhere after that, in an extension for example it's off 4 hours exactly. I've set the timezone properly in php.ini and in Magento itself so not sure what's causing it to be off. I'm running version 1.7.0.2.

EDIT

Ok so I've learned a few things.

Magento always sets the timezone to UTC in app/mage.php

line 767: date_default_timezone_set('UTC')

So, basically you can't use date(),time() etc. You have to set your targetting timezone in Admin->System->Configuration / General->Locale options and use something like:

$now = Mage::getModel('core/date')->timestamp(time());
echo date('m/d/y h:i:s', $now);

I could always replace line 767 with my timezone but I don't like modifying core code. Are there any other options out there ???

like image 724
ringerce Avatar asked Sep 30 '12 02:09

ringerce


1 Answers

The preferred method in Magento seems to be using either Zend or Varien date-time objects instead of scalar values as in this example. Once you have your object you can easily convert it to other, non-server timezones with a setTimezone call.

// a more complete example
$datetime = Zend_Date::now();
// admin controls this output through configuration
$datetime->setLocale(Mage::getStoreConfig(
             Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE))
         ->setTimezone(Mage::getStoreConfig(
             Mage_Core_Model_Locale::XML_PATH_DEFAULT_TIMEZONE));
echo $datetime->get(Zend_Date::DATETIME_SHORT);
like image 52
clockworkgeek Avatar answered Nov 06 '22 19:11

clockworkgeek