This code:
$timestamp = 1423522800; // Timestamp for 2015-02-10
$date1 = DateTime::createFromFormat('U', $timestamp);
echo $date1->format('d/m/Y H:i') . "\n";
echo $date1->format('U') . "\n\n";
$date2 = new DateTime('2015-02-10');
echo $date2->format('d/m/Y H:i') . "\n";
echo $date2->format('U') . "\n";
Gives me this output:
09/02/2015 23:00
1423522800
10/02/2015 00:00
1423522800
What the hell is going on?
I think it's timezone related, but from the DateTime::createFromFormat() documentation:
Note: The timezone parameter and the current timezone are ignored when the time parameter either contains a UNIX timestamp (e.g. 946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).
I'm using PHP 5.5.9-1ubuntu4.5
$date1is created without any TZ assumptions, but once you start callingformat(), you'll be implicitly applying TZ data, so your time-less date will get mangled into whatever DateTime is using for your TZ, as if the original date/time was UTC.date_default_timezone_set($tz);? I just ran it with$tz="America/Los_Angeles"and got 09/02/2015 23:00: 1423522800, 10/02/2015 00:00: 1423555200, respectively.date_default_timezone_set('UTC')at the begining of the script the result is the same.