3

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?

  • In db, the order time is record in UTC or in local time? – Nolwennig Nov 14 '17 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 '17 at 10:51
  • it's server time ? – Nolwennig Nov 14 '17 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 '17 at 21:48
  • In magento.stackexchange.com/a/201375/24845 xml file. What last upper A value mean in dateFormat item? – Nolwennig Nov 15 '17 at 14:33
6

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..
    }
}
1

I've resolved with:

$timefc = $objectManager->create('\Magento\Framework\Stdlib\DateTime\TimezoneInterface');
$timefc->formatDateTime($product->getCreatedAt())

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.