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.

http://ideone.com/nD7piL

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
Share:
151,616
wow
Author by

wow

Help me (-_-;)

Updated on October 08, 2021

Comments

  • wow
    wow over 2 years

    How do I convert my time from 2010-12-30 23:21:46 to ISO 8601 date format? (-_-;)

  • wow
    wow about 13 years
    Question, the output is 2010-12-30T23:21:46+1100 how to make it to be 2010-12-30T23:21:46+11:00?
  • user3167101
    user3167101 about 13 years
    @wow Try this preg_replace('/(?<=\d{2})(?=\d{2}$)/', ':', '2010-12-30T23:21:46+1100'). It outputs 2010-12-30T23:21:46+11:00.
  • Gordon
    Gordon about 13 years
  • user3167101
    user3167101 about 13 years
    @Gordon Interesting, I never knew that.
  • wow
    wow about 13 years
    date('c', strtotime('2010-12-30 23:21:46')) nice @Gordon :)
  • Ja͢ck
    Ja͢ck about 10 years
    Note the slight difference with DATE_ISO8601; +0X:00 vs +0X00.
  • ckm
    ckm about 9 years
    I 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
    rybo111 almost 9 years
    @Ja͢ck does this need down-voting, then? A slight difference is usually a bad idea.
  • Ja͢ck
    Ja͢ck almost 9 years
    @rybo111 I don't see a reason to down-vote this answer; technically, both 'c' and DATE_ISO8601 produce valid ISO8601 time representations.
  • Ja͢ck
    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
    trante almost 9 years
    I added a note to answer.
  • Kevin Morse
    Kevin Morse almost 9 years
    DATE_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
    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.iso8‌​601
  • Tushar Gaurav
    Tushar Gaurav about 7 years
    How 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
    Arahman about 7 years
    I 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
    Sushant Pachipulusu over 6 years
    Similar solution is mentioned in the comments given to the accepted answer
  • Captain Obvious
    Captain Obvious almost 6 years
    @Ja͢ck DATE_ISO8601 does not produce a valid ISO8601 representation. DATE_ATOM does though
  • Ja͢ck
    Ja͢ck almost 6 years
    @LeeButler is there a bug report for reference?
  • Captain Obvious
    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
    Ja͢ck almost 6 years
    @LeeButler with that naming of the constant, it seems reason enough to classify it as a bug
  • tomazahlin
    tomazahlin almost 6 years
    I 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
    mickmackusa over 3 years
    Using DATE_ATOM and c were already posted on this page years earlier.
  • ninsky
    ninsky over 3 years
    Keep in mind that strtotime() will convert a date according to the PHP timezone setting
  • rkok
    rkok over 2 years
    Note for JetBrains IDE users: ATOM is currently incorrectly marked as removed since PHP 7.2. Bug report: youtrack.jetbrains.com/issue/WI-59894