7

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.

2
  • How about your issue? Feb 19, 2017 at 10:58
  • Not yet solved.. Still trying with other ways Feb 20, 2017 at 12:27

7 Answers 7

5

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');
4

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);
0
3

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);
2

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');
2
$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
            );
        }
1

Try this:

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

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/

1
  • Thank u @Mr.Lewis. I will go through it.. Aug 23, 2017 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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