Date codes not agreeing. MySQL and PHP.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Steve Smith
Forum Newbie
Posts: 4
Joined: Sat Apr 05, 2003 4:36 pm
Location: Michigan State University

Date codes not agreeing. MySQL and PHP.

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
Steve Smith
Forum Newbie
Posts: 4
Joined: Sat Apr 05, 2003 4:36 pm
Location: Michigan State University

Post 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.
Post Reply