PHP’s DateTime hates handling variables directly
I’m working on an app and found that this keeps failing saying “DateTime __construct() Failed to parse time string (xxxxxxxx) at position x”
Code was as follows:
$epoch = 1447258140;
$dt = new DateTime($epoch); // This was the failing point
$timestamp = $dt->format('Y-m-d H:i:s');
But I learned here: http://stackoverflow.com/questions/17427503/php-datetime-construct-failed-to-parse-time-string-xxxxxxxx-at-position-x
That it’s best to just create the DateTime, then use one of its methods to set the time:
$epoch = 1447258140;
$dt = new DateTime();
$dt->setTimestamp($epoch);
$timestamp = $dt->format('Y-m-d H:i:s');
That worked wonderfully!