Magento2 Get Current Store Date Time

By | May 11, 2016

As we know coding Standard for Magento 2.x changed as compared to Magento 1.x

so in order to get the Date and Time in  Magento 2.x

we need to inject in our 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;
….
}

and the we can use in our Function
$date = $this->date->gmtDate();

$date = $this->date->date();

$date = $this->date->date(‘Y-m-d H:i:s’);

hope this will help you.


Leave a Reply