How to convert date time to ISO 8601 format in PHP?

Asked

Viewed 672 times

0

I have the following format: 2018-10-23T12:05:18.18UTC0TUE

I need to turn into 2013-06-28T08:54:00.000-03:00 (iso 8601 format).

There is something specific in php that helps me to transform this datetime?

What would that be 000-03:00 ?

  • https://answall.com/questions/26976/buscar-data-7-dias-antes-da-data-actual-php/26982#26982

  • https://answall.com/questions/59580/como-converter-uma-data-para-esse-formato-em-php/59600#59600

1 answer

3


Just start the constructor of DateTime passing your string as parameter:

$date = new DateTime('2018-10-23T12:05:18.18UTC0TUE')

And use the method DateTime::format to format in ISO8601

$date->format(DateTime::ISO8601);

It is possible to use the function date also:

date(DATE_ISO8601, strtotime('2018-10-23T12:05:18.18UTC0TUE'))

What would that be 000-03:00?

The 000-03:00 are actually two things together. The 000 is the end of the millisecond of your date.

The -03:00 is already referring to the Timezone of your date.

If you use the function DateTime::getTimezone(), you can get the object DateTimezone of its date.

Behold:

$date->getTimezone();

The result is:

DateTimeZone Object
(
    [timezone_type] => 3
    [timezone] => UTC
)

According to the Wikipedia:

[...] is the reference time zone from which all other time zones in the world are calculated.

Check out some date formatting constants on documentation

Browser other questions tagged

You are not signed in. Login or sign up in order to post.