Formatting an SQL timestamp with PHP

The date function expects an UNIX timestamp as its second parameter — which means you have to convert the date you get from the DB to an UNIX timestamp, which can be done using strtotime :

$db = '2010-02-20 13:14:09';
$timestamp = strtotime($db);
echo date("m-d-Y", $timestamp);

And you’ll get :

02-20-2010

You were passing the '2010-02-20 13:14:09' string to the date function ; that string is not a valid UNIX Timestamp.

'12-31-69‘ is probably 1970-01-01, in your locale ; and 1970-01-01 is the Epoch — the date that corresponds to the 0 UNIX Timestamp.

Leave a Comment