Magento 2 usually saves the time in GMT in database, but display on admin or frontend will be based on the selected timezone.
Use the below code to get the time with GMT timezone from Magento 2 code.
Magento comes with DateTime php class, Magento\Framework\Stdlib\DateTime\DateTime is used for Forms GMT date.
<?php public function __construct( \Magento\Framework\Stdlib\DateTime\DateTimeFactory $dateTimeFactory ) { $this->dateTimeFactory = $dateTimeFactory; }
Call the below function anywhere in your application like controllers, models, etc.
$dateModel = $this->dateTimeFactory->create(); echo $dateModel->gmtDate();
This gmtDate() have two parameters, first will be format and second will be optional input. Instead of current time you can specify a user defined time.
Leave a Reply