Page 1 of 1

Date codes not agreeing. MySQL and PHP.

Posted: Sat Apr 05, 2003 4:36 pm
by Steve Smith
I have dates stored in a MySQL database in 0000-00-00 format.

I am running this script...

Code: Select all

$sql = "select thought_date from thoughts order by thought_date";
$result = @mysql_query($sql) or die("could not complete your query");
$bigdate = 0;

  while($row = mysql_fetch_array($result)) {
     $date = $rowї"thought_date"];
     if($date > $bigdate){$bigdate = $date;}
	}
print (date("m.d.Y", $bigdate));
My problem is in the print line, it outputs "12.31.1969" for April 5, 2003.
I am guessing that PHP is equating "2003-04-05" to "1994" and then using that as unix timecode.

Given that "$bigdate" is "2003-04-05" how can I print "04.05.2003"?

For another application I am going to need to print "april . 05 . two thousand three" so any help there would also be appreciated.

Posted: Sat Apr 05, 2003 10:12 pm
by volka
date() takes an integer as optional second parameter. Therefor the string "2003-04-05" will be interpreted as 2003, e.g.

Code: Select all

<?php
$t = '2003-04-05';
echo (int)$t;
?>
2003 seconds, that's slighty more than half an hour ;)
maybe you're interested in http://www.mysql.com/doc/en/Date_and_ti ... tions.html and http://www.php.net/manual/en/function.strtotime.php

Posted: Sat Apr 05, 2003 11:29 pm
by Steve Smith
Thanks, you hints led me in the right direction, here is the final working script

Code: Select all

$sql = "select unix_timestamp(thought_date) as newdate from thoughts";
$result = @mysql_query($sql) or die("could not complete your query");
$bigdate = 0;



 while($row = mysql_fetch_array($result)) {
    $date = $row["newdate"];

   if($date > $bigdate){$bigdate = $date;}
}


print (date("m.d.Y", $bigdate));
Thanks again.