How to Get Current DateTime of Store

The current date and time according to the timezone of store can be found using below code snippet.

In your class file inject instance of 'Timezone' as below.

protected $_timezone;
public function __construct(
    ....
    \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
    ....
) {
    ....
    $this->_timezone = $timezone;
    ....
}


Now, in your method, you can find the current date and time of store using below statement.
$this->_timezone->date()->format('Y-m-d H:i:s');
 

No comments:

Post a Comment