How do I get a unix timestamp from PHP date time?

This is converting it to a unix timestamp: strtotime($datetime), but you're converting it back to a date again with date().


strtotime Returns a timestamp on success, FALSE otherwise.

 echo strtotime('2012-07-25 14:35:08' );

Output

1343219708

To extend the answers here with an object-oriented solution, the DateTime class must be named. The DateTime class is available since PHP 5.2 and can be used as follows.

$date = DateTime::createFromFormat('Y-m-d H:i:s', '2012-07-25 14:35:08');
echo $date->getTimestamp(); // output: 1343219708

Or even as a one-liner

echo $date = (new DateTime('2012-07-25 14:35:08'))->getTimestamp();
// output: 1343219708