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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?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