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.
<?php
/**
* @var \Magento\Framework\Stdlib\DateTime\DateTime
*/
protected $dateTime;
public function __construct(
Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
) {
$this->dateTime = $dateTime;
}
/* Convert date to timestamp usign Magento 2 Best way */
public function convertGmtTimeStamp()
{
$dateToTimestamp = "2019-05-20";
$timeStamp = $this->dateTime->gmtTimestamp($dateToTimestamp);
return $timeStamp;
}
call from template or any other function,
echo $this->convertGmtTimeStamp();
Output in timestamp:
1558310400
