I meet a very strange situation while working with event observer in Magento 2.

My custom module hooked into sales_order_save_after (which returns \Magento\Sales\Model\Order), and its magic methods work fine but getCreatedAt()

The only way to get date time in this case is using this getCreatedAtFormatted() instead of getCreatedAt().

Anyone can please tell me what was happening?

  • getCreatedAtFormatted uses getCreatedAt to create DateTime instance. So if getCreatedAt returns null and that is passed to getCreatedAtFormatted, then you are just getting current day's date not order created_at date. Apr 19, 2016 at 8:04
  • @PetarDzhambazov I don't understand. What do you mean? Apr 19, 2016 at 8:05
  • @PetarDzhambazov Yes, I read it before. But what was happnened? Why it can become null? Apr 19, 2016 at 8:09
  • I'm running into the same issue, were you ever able to resolve it? Dec 3, 2016 at 1:16

2 Answers 2


See how getCreatedAtFormatted is declared:

public function getCreatedAtFormatted($format)
    return $this->timezone->formatDateTime(
        new \DateTime($this->getCreatedAt()),
        $this->timezone->getConfigTimezone('store', $this->getStore())

It uses $this->getCreatedAt() to create DateTime and if you create DateTime like new \DateTime(null) you get current date.

The above means that if you have no value for created_at data of order, you are still going to get valid \DateTime object.

This is why getCreatedAtFormatted works and getCreatedAt is not.

Now all you need to figure out is why order is lacking created at value.

My guess is, that created_at is added by a timestamp update in the table, and to get its value, you may need to call $order->load($order->getId())


I am probably posting too late but try hooking to

'checkout_submit_all_after' instead of 'sales_order_save_after'

Just had a similar issue and found out that

setCreatedAt() is called after 'sales_order_save_after' is fired.

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.