The problem is in Mage_Eav_Model_Entity_Attribute_Backend_Time_Created::beforeSave
. This line ends up swapping the month and day:
$zendDate = Mage::app()->getLocale()->utcDate(null, $date, true, $this->_getFormat($date));
This happens to just about all entity types including products and customers, and happens during creation and normal save events, in both CE >= 1.8.x and EE >= 1.13.x.
Override this model in your own module, and replace the beforeSave
method with this:
public function beforeSave($object)
{
$attributeCode = $this->getAttribute()->getAttributeCode();
$date = $object->getData($attributeCode);
if (is_null($date)) {
if ($object->isObjectNew()) {
$object->setData($attributeCode, Varien_Date::now());
}
} else {
// ADD THIS
$date = strtotime($date);
// convert to UTC
$zendDate = Mage::app()->getLocale()->utcDate(null, $date, true, $this->_getFormat($date));
$object->setData($attributeCode, $zendDate->getIso());
}
return $this;
}
This also needs to be corrected on the way out of the database. Replace afterLoad
with this:
public function afterLoad($object)
{
$attributeCode = $this->getAttribute()->getAttributeCode();
$date = $object->getData($attributeCode);
// ADD THIS
if (!is_null($date)) {
$date = strtotime($date);
}
$zendDate = Mage::app()->getLocale()->storeDate(null, $date, true, $this->_getFormat($date));
$object->setData($attributeCode, $zendDate->getIso());
parent::afterLoad($object);
return $this;
}
Edit: Here is an example of how to override this class.
<global>
<models>
<eav>
<rewrite>
<entity_attribute_backend_time_created>Mycompany_Mymodule_Model_Eav_Entity_Attribute_Backend_Time_Created</entity_attribute_backend_time_created>
</rewrite>
</eav>
</models>
</global>
Then create the file app\code\local\Mycompany\Mymodule\Model\Eav\Entity\Attribute\Backend\Time\Created.php
created_at
and updated_at
for orders is set in Mage_Sales_Model_Resource_Abstract::_prepareDataForSave()
:
/**
* Prepare data for save
*
* @param Mage_Core_Model_Abstract $object
* @return array
*/
protected function _prepareDataForSave(Mage_Core_Model_Abstract $object)
{
$currentTime = Varien_Date::now();
if ((!$object->getId() || $object->isObjectNew()) && !$object->getCreatedAt()) {
$object->setCreatedAt($currentTime);
}
$object->setUpdatedAt($currentTime);
$data = parent::_prepareDataForSave($object);
return $data;
}
You can see that updated_at
is always set to the current time, whereas created_at
is only set if the attribute is empty. The format that it should have comes from Varien_Date
.
So you should be able to define your own creation timestamp:
$order->setCreatedAt(Varien_Date::formatDate($date, false));
where $date
can be a Unix timestamp or an arbitrary date expression that can be parsed with PHPs strtotime
Best Answer
The Magento uses time relative to the server time, transformed in UTC.
To set created_at value, try to use:
And to get local created_at value, try to use: