PHP date format converting

As of PHP5.3 you can use the DateTime API

$dt = date_create_from_format('M d, Y', 'January 23, 2010');
echo date_timestamp_get($dt);

or with OOP notation

$dt = DateTime::createFromFormat('M d, Y', 'January 23, 2010');
echo $dt->getTimestamp();

Note that while DateTime is available in PHP < 5.3, the methods used above are not and while you could simulate ->getTimestamp() with ->format('U'), there is no easy workaround for createFromFormat()

An alternative would be to use Zend_Date from Zend Framework:

$date = new Zend_Date('Feb 31, 2007', 'MM.dd.yyyy');
echo $date->get(Zend_Date::TIMESTAMP);

Leave a Comment