5

I'm working on syncing my customers with a third party application and have discovered that many of my created_at dates are way off.

Today is 2014-04-08 yet I have several records with a created_at date in the future - 2014-06-01, 2014-06-03, 2014-05-06...

I'm not exactly sure where to start troubleshooting.

This question touches on the issue, but it (and it's duplicate) are both more related to the multiple updates. I'm not seeing the created_at date changing on save/update, but I am having the date be off by days or months.

I poked around the /app/code/core/Mage/Eav/Model/Entity/Attribute/Backend/Time/Created.php and /app/code/core/Mage/Core/Model/Locale.php files but didn't see anything obviously wrong.

Does anyone have any advice about how to have accurate created_at dates? Should I comment out code similar to @alex's solution?

I'm using Magento 1.8.1.0

Timezone is CST - America/Chicago

Locale: English (United States)

8

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

  • How to map the above model in the config.xml file. When I do the mapping I'm getting an error saying Invalid backend model specified: eav/entity_attribute_backend_time_created" – User 99x Jun 9 '15 at 5:59
  • @sutha Please see my edit. – Tyler V. Jun 10 '15 at 22:04
  • it should be app\code\local\Mycompany\Eav\Model\Entity\Attribute\Backend\Time\Created.php <global> <models> <eav> <rewrite> <entity_attribute_backend_time_created>Mycompany_Eav_Model_Entity_Attribute_Backend_Time_Created</entity_attribute_backend_time_created> </rewrite> </eav> </models> </global> – Prashant Jun 15 '15 at 4:47
  • 1
    If you mirror Mage in app/code/local, you dont need to add the config as mentioned – Prashant Jun 15 '15 at 9:18
  • 2
    @prashant That's true, but then you would need to copy the entire class, and that makes upgrading Magento more tedious later. Always use event observers if possible, extending models if not, and only replace the whole class as a last resort. – Tyler V. Jun 15 '15 at 17:22
0

I also seen that in Magento EE 1.14.2.0 have issue of created date with admin user and api user, whenever we are updating admin user or api user its created date changed by current date and time. I checked and found that in database it default value was CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP , to resolve this issue make a sql file in your module and use below query to change default value

For Admin User

ALTER TABLE admin_user CHANGE COLUMN created created TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'User Created Time' ;

For API User

ALTER TABLE api_user CHANGE COLUMN created created TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'User record create date' ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.