139

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

John Slegers
  • 45,213
  • 22
  • 199
  • 169
wow
  • 7,989
  • 17
  • 53
  • 63
  • 1
    @Gordon yes I got [about 4,530 results](http://www.google.com/search?q=convert+datetime+to+ISO+8601+site:stackoverflow.com) and I found answer from @alex – wow Mar 16 '11 at 09:11
  • @wow please point out why none of the 4530 results answered your question. – Gordon Mar 16 '11 at 09:15
  • 1
    @Gordon I'm still learning how to write from A to Z but I found they teach me how to write from Z to A :) – wow Mar 16 '11 at 09:18
  • @wow No. They teach you how to write alphabet($from, $to). Almost all of them tell you to use `date($format, strtotime($dateString))` or the `DateTime` object equivalent. They only differ in the input to those functions. All you have to do is [go to the correponding pages in the PHP Manual](http://de2.php.net/manual/en/function.date.php) and find out what the input is. – Gordon Mar 16 '11 at 09:28
  • 3
    @Gordon Yes are you correct. I just knew the alphabet and now spelling bee time. Hope I'm in the right school. – wow Mar 16 '11 at 09:46

9 Answers9

281

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'));
Community
  • 1
  • 1
alex
  • 479,566
  • 201
  • 878
  • 984
  • 4
    Question, the output is `2010-12-30T23:21:46+1100` how to make it to be `2010-12-30T23:21:46+11:00`? – wow Mar 16 '11 at 08:53
  • 2
    @wow Try this `preg_replace('/(?<=\d{2})(?=\d{2}$)/', ':', '2010-12-30T23:21:46+1100')`. It outputs `2010-12-30T23:21:46+11:00`. – alex Mar 16 '11 at 08:56
  • 16
    [`echo date('c', …` produces the output with the colon.](http://stackoverflow.com/questions/3504476/help-me-with-formatting-a-date/3504534#3504534) – Gordon Mar 16 '11 at 09:29
  • @Gordon Interesting, I never knew that. – alex Mar 16 '11 at 09:43
  • 12
    `date('c', strtotime('2010-12-30 23:21:46'))` nice @Gordon :) – wow Mar 16 '11 at 22:53
  • 14
    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: https://en.wikipedia.org/wiki/ISO_8601 – ckm Mar 23 '15 at 01:42
  • @ckm Nowhere does it read that if colons are used to separate the time components, that the timezone designator must follow suit. – Ja͢ck Jun 04 '15 at 03:30
  • DATE_ISO8601 causes problems with JavaScript as ECMAScript Language Specification requires `HH:mm` http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15 – Kevin Morse Jun 07 '15 at 18:37
  • 4
    @alex, for some reason the constant ISO8601 is not compatible with ISO-8601, use `DateTime::ATOM` instead, see the note in http://php.net/manual/en/class.datetime.php#datetime.constants.iso8601 – Guilherme Sep 17 '15 at 02:49
  • 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. – Tushar Gaurav Mar 14 '17 at 11:23
  • 3
    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." – Arahman Apr 09 '17 at 09:32
  • Keep in mind that strtotime() will convert a date according to the PHP timezone setting – ninsky Jan 02 '21 at 21:16
  • 1
    Note for JetBrains IDE users: ATOM is currently **incorrectly** marked as removed since PHP 7.2. Bug report: https://youtrack.jetbrains.com/issue/WI-59894 – rkok Aug 24 '21 at 04:38
  • It's better to refer to `DateTimeInterface::ATOM` rather than a child class. – Darmen Amanbay Oct 25 '22 at 18:30
49

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.

trante
  • 33,518
  • 47
  • 192
  • 272
7

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
John Slegers
  • 45,213
  • 22
  • 199
  • 169
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
Jon B
  • 51,025
  • 31
  • 133
  • 161
Rúbia Alves
  • 61
  • 1
  • 2
4

$datetime->format('Y-m-d\TH:i:s.u\Z') should give the proper format, with the "T" separator, "Z" timezone (make sure to convert to UTC first) and microseconds (omit .u if you don't intend to support fractional seconds).

See https://stackoverflow.com/a/9532375/65387 for discussion why should use T

mpen
  • 272,448
  • 266
  • 850
  • 1,236
3

ISO 8601 is basically represented in PHP as "Y-m-d\TH:i:sP"

You can get this value from a constant:

DateTime::ATOM - for PHP versions below 7.2 (was removed)

DateTimeInterface::ATOM - for PHP versions since 7.2

Boykodev
  • 850
  • 1
  • 10
  • 23
3

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
Kristian
  • 2,456
  • 8
  • 23
  • 23
A.BEN
  • 49
  • 5
1

You can try this way:

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

echo $datetime->format(DATE_ATOM);
Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42
0

You can also get your timestamps conversion via mutation inside modal like this

class YourModal extends Model
{
    public function getCreatedAtAttribute($date)
    {
        return date(DATE_ISO8601, strtotime($date)); // ISO 8601 Date Format
    }
}