How to convert datetime with timezone to datetime. I have tried multiple which are available on PHP blog. But nothing worked out. I am retrieving datetime from MySql query as 2017-01-20T13:59:19+03:00 format. But it shows the time zone separately as +03:00. Actually the correct time is 2017-01-20 16:59:19.

So how can I show the correct time with adding GMT time in the datetime stamp.

  • How about your issue? – Khoa TruongDinh Feb 19 '17 at 10:58
  • Not yet solved.. Still trying with other ways – Pavan Kumar Feb 20 '17 at 12:27

1) We can use the PHP basic: http://php.net/manual/en/datetime.settimezone.php

<?php

$datetime = new DateTime('2017-01-20T13:59:19+03:00', new DateTimeZone('Europe/London'));

$datetime->setTimezone(new DateTimeZone('America/New_York'));
print $datetime->format('Y-m-d H:i:s (e)');
//Output 2017-01-20 05:59:19 (America/New_York)

Or

$date = '2017-01-20T13:59:19+03:00';
$convertdate = (new \DateTime())->setTimestamp(strtotime($date));
$d = $convertdate->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
//Output string '2017-01-20 10:59:19'

2) Magento provides some standard date time classes which we can find under vendor/magento/framework/Stdlib/DateTime.

For example: Magento\Framework\Stdlib\DateTime\Timezone

Inject \Magento\Framework\Stdlib\DateTime\TimezoneInterface in our constructor:

protected $timezone;

public function __construct(
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
......
)
{
    $this->timezone = $timezone;
}

We can use:

$this->timezone->date($date)->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);

instantiate

protected $_date;
 public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $date)
{
  $this->_date =  $date;
}

and use in your function

$this->_date->date()->format('d-m-Y H:i:s');

You can easily get Current Store Date Time by injecting in your class constructor in instance of \Magento\Framework\Stdlib\DateTime\TimezoneInterface and use that one to get the DateObject.

For example:

protected $timezone;
public function __construct(
    ....
    \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
    ....
) {
    ....
    $this->timezone = $timezone;
    ....
}

And then you can use it as followed:

$date = $this->timezone->formatDate();

For more information about different formats you can take a look at this article I wrote https://codeblog.experius.nl/magento-2-get-current-store-date-time/

  • Thank u @Mr.Lewis. I will go through it.. – Pavan Kumar Aug 23 '17 at 4:30

Try This

protected $timezone;

public function __construct(
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
......
)
{
    $this->timezone = $timezone;
}

$dateTimeZone = $this->timezone->date(new \DateTime($yourdate))->format('m/d/y H:i:s');
$date = '2018-01-29 13:10:20';
    $this->formatDate(
                    $this->_localeDate->date(new \DateTime($date)),
                    \IntlDateFormatter::MEDIUM,
                    true
                );
        public function formatDate(
            $date = null,
            $format = \IntlDateFormatter::SHORT,
            $showTime = false,
            $timezone = null
        ) {
            $date = $date instanceof \DateTimeInterface ? $date : new \DateTime($date);
            return $this->_localeDate->formatDateTime(
                $date,
                $format,
                $showTime ? $format : \IntlDateFormatter::NONE,
                null,
                $timezone
            );
        }

Try this:

$timezoneInterface = new \Magento\Framework\Stdlib\DateTime\TimezoneInterface;
$dateTimeZone = $timezoneInterface->date($yourDate)->format('m/d/y H:i:s');

This one works for me

\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
$this->timezone = $timezone

$localizedDateTimeISO = $this->timezone->date(new \DateTime($repliedAt))->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.