How do I convert datetime to ISO 8601 in PHP

wow picture wow · Mar 16, 2011 · Viewed 124.7k times · Source

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

Answer

alex picture alex · Mar 16, 2011

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