How to convert datetime to ISO 8601 in PHP

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

+57
php datetime
Mar 16 2018-11-11T00:
source share
4 answers

Object oriented

This is the recommended method.

 $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 prefer the procedural code.

 echo date(DATE_ISO8601, strtotime('2010-12-30 23:21:46')); 
+144
Mar 16 2018-11-11T00:
source share
— -

After PHP 5, you can use this: echo date("c"); ISO 8601 form in datetime format.

http://ideone.com/nD7piL

Note for comments:

As for this , both of these expressions are valid for the time zone, for the basic format: ±[hh]:[mm], ±[hh][mm], or ±[hh] .

But keep in mind that + 0X: 00 is true, and + 0X00 is incorrect for extended use. Therefore, it is better to use date("c") . A similar discussion is here .

+28
Jun 23 '12 at 21:17
source share

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 (time zone 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 (user time zone):

 date_format(date_timestamp_set(new DateTime(), 1326883500)->setTimezone(new DateTimeZone('America/New_York')), 'c'); // Output : 2012-01-18T05:45:00-05:00 
+1
Apr 03 '17 at 18:32
source share

If you try to set the value to datetime-local

date ("Ymd \ TH: i", strtotime ('2010-12-30 23:21:46'));

// output: 2010-12-30T23: 21

-one
Nov 13 '17 at 17:23
source share



All Articles