I have an extension attribute for a dateTime field in sales_order table.

What I want to do on a certain action is to set the value of this field to NOW().

How can I perform this?

$order->setData('my_date_time_field', WHAT_SHOULD_I_WROTE_HERE?);

Now I am using the Magento stdLib\DateTime class to get the data, I just want sql to use it's own NOW() function.


2 Answers 2


You can inject \Magento\Framework\Stdlib\DateTime\TimezoneInterface in Constructor and use it's instance to get current date as below:

$this->_date->date()->format('Y-m-d H:i:s');

Note: I have created $_date instance for TimezoneInterface


You can try using a Zend_Db_Expr object. The following code should creat the MySQL statement the way you want:

$order->setData('my_date_time_field', new \Zend_Db_Expr('NOW()'));

I hope that helps, feedback would be nice :-)

Your Answer

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

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