I am trying to put a timeago in each post data inside an array , the post data are from mysql table. the code as follow:
$sql = "SELECT * FROM posts ORDER BY time DESC LIMIT 8";
$query = mysql_query($sql)or die(mysql_error());
$count = 0; // Initialize counter
$rows = array();
while($row = mysql_fetch_array( $query )) {
$rows[++$count] = $row;
}
The rows have been put into an array.
inside each there is : user , postcontent , time(which i use to calculate timegao)
timeago script as follows:
function timeago($time)
{
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$format = strtotime($time);
$difference = $now - $format;
$tense = "ago";
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++)
{
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1)
{
$periods[$j].= "s";
}
return "$difference $periods[$j] $tense";
}
how do i loop through each array row , get the time out , calculate it using timeago function , then inject in the results as ‘timeago’:’value-calculated’ into each array row?
Like below, PS:
$counteris not necessary.