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.
Did this post help you?
Tutsplanet brings in-depth and easy tutorials to understand even for beginners. This takes a considerable amount of work. If this post helps you, please consider supporting us as a token of appreciation:
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.
Leave a Reply