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?

How to retrieve GMT Date Time from Magento 2?

Magento native supports DateTime PHP class, Magento\Framework\Stdlib\DateTime\DateTime is used for forms GMT date.

Magento provides GMT timezone by using the given function in the code snippet.

<?php
public function __construct(\Magento\Framework\Stdlib\DateTime\DateTimeFactory $dateTimeFactory)
{
    $this->dateTimeFactory = $dateTimeFactory;
}

Call the above function at the required place and your work is done,

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

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

Result: 2023-04-03 11:22:49