user1548981 user1548981 - 7 months ago 8
PHP Question

getTimestamp is not giving the correct hour

I have this php code

$from_date = new DateTime('April-22-2016');
$from_date->format( 'Y-m-d 00:00:00' );
$from_date->setTime(0,0,0);
print ' / '. $from_date_unix = $from_date->getTimestamp();


the above code print a unix time stamp of 1461356160 which means
GMT: Fri, 22 Apr 2016 20:16:00 GMT

The hours are 20:16:00 - which is strange since i already set the time to 00:00:00 via

$from_date->setTime(0,0,0);


Is my code wrong ? Do i need to check something on php.ini ?

Answer

You can parse the DateTime object with the right format with DateTime::createFromFormat

Use it like this:

$from_date = DateTime::createFromFormat('F-d-Y', 'April-22-2016');
$from_date->format( 'Y-m-d 00:00:00' );
print ' / '. $from_date_unix = $from_date->getTimestamp();
Comments