How to Convert Datetime to Iso 8601 in PHP

How do I convert datetime to ISO 8601 in PHP

Object Oriented

This is the recommended way.

$datetime = new DateTime('2010-12-30 23:21:46');

echo $datetime->format(DateTime::ATOM); // Updated ISO8601


Procedural

For older versions of PHP, or if you are more comfortable with procedural code.

echo date(DATE_ISO8601, strtotime('2010-12-30 23:21:46'));

Convert DateTime to ISO 8601 DateTime Format with timezone

If you wish to output format in Zulu/UTC/GMT timezone, then you must first convert timezone, since I do not know in which timezone is your current setup.

$dt = new DateTime('2010-12-30 23:21:46');
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('Y-m-d\TH:i:s.u\Z');

demo


And ISO-8601 is a standard that has many variations of format:

  • 2018-04-03
  • 2018-04-03T05:20:03+00:00
  • 2018-04-03T05:20:03Z
  • 20180403T052003Z
  • 2018-W14
  • 2018-W14-2
  • ...

This are all valid ISO-8601 formats. Read more here.

Convert Time to ISO 8601 format in UTC timezone

If you want the ISO 8601 format you should use the DateTimeInterface::ISO8601 in the format method, or you can use "c":

$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0000");
echo $date->format(\DateTime::ISO8601);
// will be 2019-03-15T08:56:57+0000
echo $date->format("c");
/*
will be 2019-03-15T08:56:57+00:00
note the : in between hh and mm in offset(timezone)
generally accepted as valid ISO 8061 see:
https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
*/

Regarding the timezone if you want to force it into UCT timezone then you should use the setTimezone method on the date object first with timezone param "UTC":

$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0000");
$date->setTimezone(new DateTimeZone("UTC"));
$output = $date->format(\DateTime::ISO8601);

Note about the above that if your original date time is not in UTC(has an offset) the time will be converted to UTC and the offset will be 0000:

$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0230");
echo $date->format(\DateTime::ISO8601);
// will be: 2019-03-15T08:56:57+0230
$date->setTimezone(new DateTimeZone("UTC"));
echo $date->format(\DateTime::ISO8601);
// will be: 2019-03-15T06:26:57+0000

How to convert ISO8601 to Date format in php

try this

$date = '2014-03-13T09:05:50.240Z';

$fixed = date('Y-m-d', strtotime($date));

The complete date function documentation can be found here: http://php.net/manual/en/function.date.php

The PHP function "strtotime" does nothing else then converting your timestring into an unix timestamp.

Hope I could help :)

P.s.:
Just in case strtotime will return 0 try using this:

$date = '2014-03-13T09:05:50.240Z';

$fixed = date('Y-m-d', strtotime(substr($date,0,10)));

Convert String (ISO) to Date PHP

You can try like this:-

<?php

$time = "2016-07-16T1:22:04.324+1030";

echo date("d M, Y",strtotime(date($time)));

?>

Output:- 16 Jul, 2016 :- https://eval.in/594695

Reference taken:-

Converting ISO 8601 format to d M Y in PHP

Note:- you can change format of date according to your convenience. like D M d Y h:i:s O T . I checked this code on PHP 5.5.4 , PHP 5.5.14, PHP 7

PHP Convert ISO date to more readable format?

$format = "d M Y"; //or something else that date() accepts as a format
date_format(date_create($time), $format);

How to convert MySQL date time into ISO 8601 date format without offset?

You have to build the date format with the formatting parameter you need.

date(DATE_ISO8601, strtotime('2012-04-13 09:45:36')); is the same as date('Y-m-d\TH:i:sO', strtotime('2012-04-13 09:45:36'));. If you remove the O parameter (offset to GMT) you will get the result you need:

date('Y-m-d\TH:i:s', strtotime('2012-04-13 09:45:36'));



Related Topics



Leave a reply



Submit