2

I have custom table and attachment is a structure of the database. enter image description here I Save data with

$post_data['created_date'] = now();
$model = Mage::getModel("rapnet/rapnet")->addData($post_data)->save();

Created date is now() and updated date is "CURRENT_TIMESTAMP". So why both are different on my localhost? How can we solve this to keep both same while creating a new record?

2 Answers 2

4

I had this issue early (it because your database set to one locale, but server to another).

So, here is a solution.

First: don't use now() function, use Mage::getModel('core/date')->timestamp(); in Magento.

Second: don't use CURRENT_TIMESTAMP as a default value for your magento database. Set both created_at and updated_at fields to DATETIME type.

Just use this upgrade script:

$installer = $this;
$table = $installer->getTable('model/entity'); // values from your config.xml here
$installer->startSetup();
$table->addColumn($table, 'created_at', [
    'type'     => Varien_Db_Ddl_Table::TYPE_DATETIME,
    'comment'  => 'created_at',
]);
$table->addColumn($table, 'updated_at', [
    'type'     => Varien_Db_Ddl_Table::TYPE_DATETIME,
    'comment'  => 'updated_at'
]);
$installer->endSetup();

or this (if you already have these columns):

$installer = $this;
$table = $installer->getTable('model/entity'); // values from your config.xml here
$installer->startSetup();
$table->modifyColumn($table, 'created_at',[
    'type'     => Varien_Db_Ddl_Table::TYPE_DATETIME,
    'comment'  => 'created_at',
]);
$table->modifyColumn($table, 'updated_at',[
    'type'     => Varien_Db_Ddl_Table::TYPE_DATETIME,
    'comment'  => 'updated_at'
]);
$installer->endSetup();

Third: in your model add function _beforeSave()(and you shouldn't set date manually anymore) and in that function make next logic: if it just created set created_at to current time using function above, and set updated_at to current time.

It looks like:

public function _beforeSave()
{
    if($this->isObjectNew()){
        $this->setCreatedAt(Mage::getModel('core/date')->timestamp());
    }
    $this->setUpdatedAt(Mage::getModel('core/date')->timestamp());
    return parent::_beforeSave();
}

Fourth: also it is good practice in magento to call your columns created_at and updated_at.

Hope it will be helpful for you :)

1
  • don't you want to use Mage::getSingleton('core/date')->gmtDate() instead? as Mage::getModel('core/date')->timestamp() uses the magento timestamp
    – ben.incani
    Apr 16, 2019 at 23:17
0

This is timezone issue, mysql save CURRENT_TIMESTAMP in UTC format while your application time() may be picking your server timezone. You need to keep timezone same for both column. For more see here

2
  • Bro. I know this is a timezone issue. The difference is 5 hours. Updated date is CURRENT_TIMESTAMP which is inserted with Mysql.And Php function is behind with 5:30Hours. And my IST time is also -5:30. But I am in localhost so how it's possible for this? Sep 8, 2017 at 5:39
  • 1
    Bro localhost is also server, You need to check timezone on your localhost eg. echo date_default_timezone_get(); and manage then you can update according to your need
    – Arunendra
    Sep 8, 2017 at 5:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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