Hi,
I've been trying very hard to insert a "how long ago" type of timestamp in my code. I've tried many different functions found in differnt sites, particularly at php.net.
Since I am getting some results with the functions (but not not what I expected) I have reason to believe the problem are not the functions but the way I am using them.
The one below, for example, was supposed to give me something like: 7 hours, 15 minutes, 4 seconds ago. But instead I am getting: -20069521402986 seconds ago.
I have the MySQL date collumn set as TIMESTAMP.
Any help would be appreciated.
Tim
<?php
$hostname="someserver.net";
$username="someusername";
$password="somepassword";
$dbname="someusername";
$usertable="sometable";
function ago($timestamp){
$difference = time() - $timestamp;
$periods = array("second", "minute", "hour", "day", "week", "month", "years", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
for($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] ago";
return $text;
}
mysql_connect($hostname,$username,$password) or die(mysql_error());
mysql_select_db("$dbname") or die(mysql_error());
$data = mysql_query("SELECT id, title, date FROM $usertable ORDER BY ID DESC LIMIT 7")
or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
echo "<a href=article-".$info['id'].".htm>" .$info['title']."</a>".ago($info['date']);
echo "<br>";
}
?>