Magento 2 convert date to timestamp using best practice.

Magento 2 You can Converts input date into GMT timestamp using Magento\Framework\Stdlib\DateTime\DateTime Class.

You can set easily convert timestamp using core PHP by function strtotime().
like, strtotime(‘+1 years’);

Using Magento You can Converts input date into GMT timestamp by using Magento best practice way by injecting class dependency in the construct method. Continue reading “Magento 2 convert date to timestamp using best practice.”

Magento 2 Get Date after 1 year period.

Magento 2 you can get a date after 1 year period using Magento\Framework\Stdlib\DateTime\DateTime Class.

You can get easily using PHP by function date().
date(‘Y-m-d’, strtotime(‘+1 years’));

Using Magento You can get a date after 1 year by using Magento best practice way by injecting class dependency in the construct method. Continue reading “Magento 2 Get Date after 1 year period.”

How to get Current GMT date time in Magento 2?

You can get GMT Timezone in Magento by below code snippet,
Magento gives GMT timezone based on below function, Magento comes with DateTime php class, Magento\Framework\Stdlib\DateTime\DateTime is used for Forms GMT date.

Call from template file or php file by below way,

$dateModel = $this->dateTimeFactory->create();
echo $dateModel->gmtDate();

First Parameter is optional $format = null in gmtDate()
Default Format is Y-m-d H:i:s, You can add your time zone to convert normal timezone to GMT Timezone.

Result: 2018-09-09 11:22:49