How to get Current GMT date time in Magento 2?

You can get GMT Timezone in Magento by below code snippet,
Magento gives GMT timezone based on below function, Magento comes with DateTime php class, Magento\Framework\Stdlib\DateTime\DateTime is used for Forms GMT date.

Call from template file or php file by below way,

$dateModel = $this->dateTimeFactory->create();
echo $dateModel->gmtDate();

First Parameter if gmtDate() is optional $format = null.
Default Format is Y-m-d H:i:s, You can add your time zone for convert normal timezone to GMT Timezone.

One Reply to “How to get Current GMT date time in Magento 2?”

  1. I have added this “echo $dateModel->gmtDate();” in Jquery variable but If I page refresh that Time show old. See below

    $gmtDate=$dateModel->gmtDate();
    I have set “$currentDate=Date(“m/d/y h:i:s A”, strtotime($gmtDate));” in page.
    when i page open $currentDate is “02/07/19 09:15:58 AM”.
    After 5 mins I have page refresh $currentDate should be “02/07/19 09:20:58 AM”. but it show “02/07/19 09:15:58 AM”.
    If I cache clear that its work fine.

    Can you please tell me why this happen? And how to resolve it?
    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *