In Magento 1.x you could get the store date time via
Mage::getModel('core/date')->gmtDate();
What would be the equivalent to this in Magento 2.x?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityIn Magento 1.x you could get the store date time via
Mage::getModel('core/date')->gmtDate();
What would be the equivalent to this in Magento 2.x?
You need to inject in your class constructor an instance of \Magento\Framework\Stdlib\DateTime\DateTime
and use that one.
Something like this:
protected $date;
public function __construct(
....
\Magento\Framework\Stdlib\DateTime\DateTime $date,
....
) {
....
$this->date = $date;
....
}
Then, you can use in your class this:
$date = $this->date->gmtDate();
gmtDate
shown above accepts 2 optional parameters. The first one is $format
which defaults to Y-m-d H:i:s
. You can just call the method with the parameter you want gmtDate('H:i:s')
or any other time format.
To get UTC date in Magento2 you should use \Magento\Framework\Stdlib\DateTime\DateTime::gmtDate();
You should inject dependency on this class via construct and then use this function. See this class for more date/time related methods.
In your code sample you are retrieving UTC date, not store date.
To get date formatted according to the timezone of the current store, use
Magento\Framework\Stdlib\DateTime\TimezoneInterface::formatDate();
(again, by injecting dependency to construct)
\Magento\Framework\Stdlib\DateTime\DateTime::gmtTimestamp()
Feb 1, 2016 at 15:14
You can easily get Current Store Date Time by injecting in your class constructor in instance of \Magento\Framework\Stdlib\DateTime\TimezoneInterface
and use that one to get the DateObject.
For example:
protected $timezone;
public function __construct(
....
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
....
) {
....
$this->timezone = $timezone;
....
}
And then you can use it as followed:
$date = $this->timezone->formatDate();
For more information about different formats you can take a look at this article I wrote https://codeblog.experius.nl/magento-2-get-current-store-date-time/
Magento 2.x have context objects for different classes, if you are in a context of Block then context object can give you locale date object as follows:
/**
* Locale Date/Timezone
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
*/
protected $_timezone;
/**
* @param \Magento\Catalog\Block\Product\Context $context
* @param array $data
*/
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
array $data = []
) {
$this->_timezone = $context->getLocaleDate();
parent::__construct(
$context,
$data
);
}
then you can use it like following:
$todayDate = $this->_timezone->date()->format('Y-m-d H:i:s');
This will avoid errors while executing di:compile command.
To get the current date time of a particular store (other than current store in StoreManager):
Reference from \Magento\Framework\Stdlib\DateTime\Timezone::convertConfigTimeToUtc()
/** @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone */
/** @var \Magento\Framework\Stdlib\DateTime\Timezone $timezone */
$timezone = $this->timezone->getConfigTimezone(\Magento\Store\Model\ScopeInterface::SCOPE_STORES, $storeId);
$currentDate = new \DateTime('now', new \DateTimeZone($timezone));
var_dump($currentDate->format('Y-m-d H:i:s'));
\Magento\Framework\Stdlib\DateTime
will get you UTC date time, GMT date time or current store's date time.
Magento 2 sets UTC in app/bootstrap
:
date_default_timezone_set('UTC');
\DateTime
uses this PHP timezone setting by default.
Magento 2 will use internally UTC and it saves in MySQL in UTC also. Linux servers and MySQL servers usually are set to UTC timezone. The chain of the timezone settings on a server is not in scope of this topic.
Magento 2 will display on frontend the date in current store's timezone using locale resolver \Magento\Framework\Locale\Resolver
to get the current store timezone (e.g Europe/Bruxelles
).
We can set store timezone using observer with event "controller_action_predispatch"
Create events.xml in Mymodle/etc/frontend/events.xml folder
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch">
<observer name="mymodule_timezone_set" instance="MyNamespace\Mymodule\Observer\SetStoreTimezoneObserver" />
</event> </config>
In Observer folder create file SetStoreTimezoneObserver.php
<?php
namespace MyNamespace\Mymodule\Observer;
use Magento\Framework\Event\ObserverInterface;
class SetStoreTimezoneObserver implements ObserverInterface
{
protected $_storeTime;
protected $_storeManager;
public function __construct(
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
\Magento\Store\Model\StoreManagerInterface $storeManager
)
{
$this->_storeTime = $timezone;
$this->_storeManager = $storeManager;
$this->setStoreTimezone();
}
/**
* Retrieve store model instance
*
* @return \Magento\Store\Model\Store
*/
public function getStore()
{
return $this->_storeManager->getStore();
}
/*
* Set Store Timezone
*/
public function setStoreTimezone()
{
date_default_timezone_set(
$this->_storeTime->getConfigTimezone('store', $this->getStore())
);
}
/**
* Predispath admin action controller
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->setStoreTimezone();
}
}
Now instead of getting "UTC" date, we get current store date using simple date("Y-m-d H:i:s") function.
In my case, if i use this on my controller, it doesn't work. I get the default locale date instead.
But if i use it on my block it works.
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
$todayDate = $this->_timezone->date()->format('Y-m-d H:i:s');