Convert Local date to UTC Date timezone by Magento 2 using LocalizedDateToUtcConverter Interface.
This article will help to convert specific timezone to UTC time zone.
You need to instantiate the LocalizedDateToUtcConverterInterface
interface to the __construct()
method of your class.
<?php namespace Jesadiya\UtcDate\Model; use Magento\Framework\Stdlib\DateTime\Timezone\LocalizedDateToUtcConverterInterface; class UtcDate { /** * @var LocalizedDateToUtcConverterInterface */ private $utcConverter; public function __construct( LocalizedDateToUtcConverterInterface $utcConverter ) { $this->utcConverter = $utcConverter; } /** * convert UTC Date * * @return string */ public function convertUTCdate(): string { $localDate = date('m-d-Y'); return $this->utcConverter->convertLocalizedDateToUtc($localDate); } }
Call method,
echo $utcDate = $this->convertUTCdate();
Output:
2020-09-29 19:06:22