8

I am trying to get the order time, I fixed the problem with orders with wrong time stamp in admin panel. Now I am trying to get the order date in my module.

The problem is

$created = $order->getCreatedAt() 

returns the UTC time and not local time. I tried to use

$created = $order->getCreatedAtStoreDate(); 

But this one returns nothing. How should I get the orders date?

6
  • In db, the order time is record in UTC or in local time?
    – Nolwennig
    Nov 14, 2017 at 10:48
  • @Nolwennig yes it is correct also in db, but for some reason it is always 2 hours behind with $order->getCreatedAt()
    – Korbin
    Nov 14, 2017 at 10:51
  • it's server time ?
    – Nolwennig
    Nov 14, 2017 at 14:38
  • no the server time is correct, also admin panel time is correct. Somehow in between something happens that api returns wrong time to me
    – Korbin
    Nov 14, 2017 at 21:48
  • In magento.stackexchange.com/a/201375/24845 xml file. What last upper A value mean in dateFormat item?
    – Nolwennig
    Nov 15, 2017 at 14:33

2 Answers 2

11

You can use an instance of Timezone to convert it into store's timezone. Here is a very generic example.

namespace VendorName\ModuleName\MyDir;

class MyClass
{
    private $timezone;

    public function __construct(
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
    ) {
        $this->timezone = $timezone;
    }

    public function myMethod($order)
    {
        $created = $order->getCreatedAt();

        //Convert to store timezone
        $created = $this->timezone->date(new \DateTime($created));

        //To print or display this you can use following.
        //Feel free to tweak the format
        $dateAsString = $created->format('M j, Y g:i:s A');

        //Proceed further..
    }
}
0

I've resolved with:

$timefc = $objectManager->create('\Magento\Framework\Stdlib\DateTime\TimezoneInterface');
$timefc->formatDateTime($product->getCreatedAt())
2
  • It showing error for me. Jun 20, 2020 at 13:46
  • 1
    Object manager is not the way.
    – Korbin
    Feb 8, 2021 at 14:24

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.