How to Convert Iso8601 to Date Format in PHP

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

How do I convert an ISO8601 date to another format in PHP?

A fast but sometimes-unreliable solution:

$date = '2011-09-02T18:00:00';

$time = strtotime($date);

$fixed = date('l, F jS Y \a\t g:ia', $time); // 'a' and 't' are escaped as they are a format char.

Format characters detailed 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

Converting ISO 8601 format to d M Y in PHP

You need to use the strtotime() function.

echo date("d M, Y",strtotime(date("c")));

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 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.

Carbon parse Iso 8601 string to UTC date and record it to db

You don't need to pass time zone as a second parameter to parse function. Time zone is already part of date string. If you need to save date in UTC just convert it to UTC timezone after parsing like so:

Carbon::parse("2018-03-21T10:34:15-0200")->setTimezone('UTC')

Converted date will be: 2018-03-21 12:34:15.0 UTC (+00:00)



Related Topics



Leave a reply



Submit