Strtotime keeps returning false on MySql timestamp

Associate
Joined
19 Jun 2006
Posts
162
Location
Swansea, Wales
I posted this on devshed but it seems no-one is there tonight. should have learnt by now to always stick with the tried and tested ocuk :p

After reading plenty of different sites I can't seem to come up with a solution for my problem. My code...

Code:
 $ts = strtotime("Y-m-d H:i:s", $row["timestamp"]);

if ($ts == false || $ts === "-1") {
  echo "Invalid timestamp.";
}
                
$date = date("d m Y", $ts);
$time = date("H:i", $ts);

The database row is definitly in the "Timestamp" format and when i view it in phpMyadmin I see a date like: "2007-05-12 22:25:55"

I don't see the problem! Help!
 
Not sure what you're trying to do with strtotime there. But that's certainly not how you use it.

int strtotime ( string $time [, int $now] )

The function expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT), relative to the timestamp given in now, or the current time if none is supplied.
 
strtotime() is self-explanatory - it creates a timestamp from a string date, not the opposite. It takes no format as it can interpret pretty much any under the Sun - and fortunately the MySQL DATETIME format, albeit only from PHP5.x onwards, I believe.

PHP:
<?php

$ts = strtotime($row['timestamp']);
$date = date('d m Y', $ts);
$time = date('H:i', $ts);

?>
 
Last edited:
Back
Top Bottom