How to Convert an Iso8601 Date to Another 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)));

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

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

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

How to display a date as iso 8601 format with PHP

The second argument of date is a UNIX timestamp, not a database timestamp string.

You need to convert your database timestamp with strtotime.

<?= date("c", strtotime($post[3])) ?>

Convert one date format into another in PHP

The second parameter to date() needs to be a proper timestamp (seconds since January 1, 1970). You are passing a string, which date() can't recognize.

You can use strtotime() to convert a date string into a timestamp. However, even strtotime() doesn't recognize the y-m-d-h-i-s format.

PHP 5.3 and up

Use DateTime::createFromFormat. It allows you to specify an exact mask - using the date() syntax - to parse incoming string dates with.

PHP 5.2 and lower

You will have to parse the elements (year, month, day, hour, minute, second) manually using substr() and hand the results to mktime() that will build you a timestamp.

But that's a lot of work! I recommend using a different format that strftime() can understand. strftime() understands any date input short of the next time joe will slip on the ice. for example, this works:

$old_date = date('l, F d y h:i:s');              // returns Saturday, January 30 10 02:06:34
$old_date_timestamp = strtotime($old_date);
$new_date = date('Y-m-d H:i:s', $old_date_timestamp);

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

Convert ISO 8601 to unixtimestamp

echo date("U",strtotime('2012-01-18T11:45:00+01:00'));


Related Topics



Leave a reply



Submit