1

I get different date from todays date. I tried below ways and all give difference in time:

1)

        \Magento\Catalog\Block\Product\Context $contexts,
..........
..........

        $this->_timezone = $contexts->getLocaleDate();
..........
..........
$this->_timezone->date()->format('Y-m-d');

2)

$obj = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Framework\Stdlib\DateTime\DateTime $dateTime */
$dateTime = $obj->create(\Magento\Framework\Stdlib\DateTime\DateTime::class);

$currentDate = $dateTime->gmtDate();

3)

 \Magento\Framework\Stdlib\DateTime\DateTime $date

As answered here, times are store in the database as UTC (+0:00). How can we set it to get correct date.

1

I got the right time as per my clock from magento as below:

->Configured Timezone from Magento Backend

Stores -> Configuration -> General ->Locale Options -> Timezone to reflect "Gulf Standard Time (Asia/Dubai)"

as per the store location.

-> Got current date and time with

public function __construct(
          ...............
        \Magento\Catalog\Block\Product\Context $contexts,
        .....................
    )
{

        $this->_timezone = $contexts->getLocaleDate();
}

public function myFunctionName(){

    return $nowDateTime = $this->_timezone->date()->format('Y-m-d H:i:s');
}
0

Get current UTC datetime:

/**
 * @var \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
 */
$dateTime->date();

Get current datetime in store timezone:

/**
 * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
 */
$timezone->date(new \DateTime())->format('Y-m-d H:i:s');

Note that all datetimes stored in Magento database are in UTC.

To debug your store datetime, you can try this code:

$currentPhpDateTime = new \DateTime();

/**
 * @var \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
 */
$currentMagentoDateTime = $dateTime->date();

/**
 * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
 */
$currentLocalizedMagentoDateTime = $timezone->date($now)->format('Y-m-d H:i:s');

dump($now->format('Y-m-d H:i:s')); // output: current datetime in UTC
dump($currentMagentoDateTime); // output: current datetime in UTC
dump($currentLocalizedMagentoDateTime); // output: current datetime in timezone you set in Magento configuration

You will see something similar to these outputs:

"2019-08-28 00:00:00"
"2019-08-28 00:00:00"
"2019-08-28 07:00:00"

The first and second outputs are current date in UTC. The third one is current date in timezone you set in Magento configuration.

Note that date constructed using new \DateTime() is in UTC, because Magento set your default timezone to UTC while bootstraping the app. In Magento 2.3.2, you can see how Magento set your default timezone in file app/bootpstrap.php line 73:

date_default_timezone_set('UTC');

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.