Creating DateTime from timestamp in PHP < 5.3

Yarin picture Yarin · Dec 1, 2010 · Viewed 74.6k times · Source

How do you create a DateTime from timestamp in versions less than < 5.3?

In 5.3 it would be:

$date = DateTime::createFromFormat('U', $timeStamp);

The DateTime constructor wants a string, but this didn't work for me

$date = new DateTime("@$timeStamp");

Answer

Dawid Ohia picture Dawid Ohia · Mar 27, 2012

PHP 5 >= 5.3.0

$date = new DateTime();
$date->setTimestamp($timeStamp);

Edit: Added correct PHP version for setTimestamp