How do I convert datetime to ISO 8601 in PHP
151,616
Solution 1
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'));
Solution 2
After PHP 5 you can use this: echo date("c");
form ISO 8601 formatted datetime.
Note for comments:
Regarding to this, both of these expressions are valid for timezone, for basic format: ±[hh]:[mm], ±[hh][mm], or ±[hh]
.
But note that, +0X:00 is correct, and +0X00 is incorrect for extended usage. So it's better to use date("c")
. A similar discussion here.
Solution 3
How to convert from ISO 8601 to unixtimestamp :
strtotime('2012-01-18T11:45:00+01:00');
// Output : 1326883500
How to convert from unixtimestamp to ISO 8601 (timezone server) :
date_format(date_timestamp_set(new DateTime(), 1326883500), 'c');
// Output : 2012-01-18T11:45:00+01:00
How to convert from unixtimestamp to ISO 8601 (GMT) :
date_format(date_create('@'. 1326883500), 'c') . "\n";
// Output : 2012-01-18T10:45:00+00:00
How to convert from unixtimestamp to ISO 8601 (custom timezone) :
date_format(date_timestamp_set(new DateTime(), 1326883500)->setTimezone(new DateTimeZone('America/New_York')), 'c');
// Output : 2012-01-18T05:45:00-05:00
Solution 4
According to PHP offcial documentation you can simply format it to:
echo $objDateTime->format('c'); // ISO8601 formated datetime
echo $objDateTime->format(DateTime::ISO8601); // Another way to get an ISO8601 formatted string
Solution 5
If you try set a value in datetime-local
date("Y-m-d\TH:i",strtotime('2010-12-30 23:21:46'));
//output : 2010-12-30T23:21
Comments
-
wow over 2 years
How do I convert my time from
2010-12-30 23:21:46
to ISO 8601 date format? (-_-;) -
wow about 13 yearsQuestion, the output is
2010-12-30T23:21:46+1100
how to make it to be2010-12-30T23:21:46+11:00
? -
user3167101 about 13 years@wow Try this
preg_replace('/(?<=\d{2})(?=\d{2}$)/', ':', '2010-12-30T23:21:46+1100')
. It outputs2010-12-30T23:21:46+11:00
. -
Gordon about 13 years
-
user3167101 about 13 years@Gordon Interesting, I never knew that.
-
wow about 13 years
date('c', strtotime('2010-12-30 23:21:46'))
nice @Gordon :) -
Ja͢ck about 10 yearsNote the slight difference with
DATE_ISO8601
;+0X:00
vs+0X00
. -
ckm about 9 yearsI would note that using
DATE_ISO8601
produces a date string which is slightly different than ISO8601 (the colon is missing in the TZ, ISO8601 expects times to be all with OR all without the colon, not a mixture) -date('c')
does produces a strict ISO 8601 valid date - This could cause hard to trace bugs if code expects a strict ISO 8601 datetime format. Ref: en.wikipedia.org/wiki/ISO_8601 -
rybo111 almost 9 years@Ja͢ck does this need down-voting, then? A slight difference is usually a bad idea.
-
Ja͢ck almost 9 years@rybo111 I don't see a reason to down-vote this answer; technically, both
'c'
andDATE_ISO8601
produce valid ISO8601 time representations. -
Ja͢ck almost 9 years@ckm Nowhere does it read that if colons are used to separate the time components, that the timezone designator must follow suit.
-
trante almost 9 yearsI added a note to answer.
-
Kevin Morse almost 9 yearsDATE_ISO8601 causes problems with JavaScript as ECMAScript Language Specification requires
HH:mm
ecma-international.org/ecma-262/5.1/#sec-15.9.1.15 -
Guilherme over 8 years@alex, for some reason the constant ISO8601 is not compatible with ISO-8601, use
DateTime::ATOM
instead, see the note in php.net/manual/en/class.datetime.php#datetime.constants.iso8601 -
Tushar Gaurav about 7 yearsHow to remove seconds from the first method. Its giving me output : 2017-03-18T11:30:00+00:00. But I need : 2017-03-18T11:30+00:00.
-
Arahman about 7 yearsI love this gem from the PHP documentation: "DATE_ISO8601 [...] Note: This format is not compatible with ISO-8601, but is left this way for backward compatibility reasons. Use DateTime::ATOM or DATE_ATOM for compatibility with ISO-8601 instead."
-
Sushant Pachipulusu over 6 yearsSimilar solution is mentioned in the comments given to the accepted answer
-
Captain Obvious almost 6 years@Ja͢ck
DATE_ISO8601
does not produce a valid ISO8601 representation. DATE_ATOM does though -
Ja͢ck almost 6 years@LeeButler is there a bug report for reference?
-
Captain Obvious almost 6 years@Ja͢ck I'm not sure, but it's documented in the Php docs that it's not quite compatible.
-
Ja͢ck almost 6 years@LeeButler with that naming of the constant, it seems reason enough to classify it as a bug
-
tomazahlin almost 6 yearsI agree with @LeeButler, the DATE_ISO8601 does not produce a valid ISO8601 string. This is very confusing, but I doubt PHP would fix that, since it would be a BC break for them.
-
mickmackusa over 3 yearsUsing
DATE_ATOM
andc
were already posted on this page years earlier. -
ninsky over 3 yearsKeep in mind that strtotime() will convert a date according to the PHP timezone setting
-
rkok over 2 yearsNote for JetBrains IDE users: ATOM is currently incorrectly marked as removed since PHP 7.2. Bug report: youtrack.jetbrains.com/issue/WI-59894