0

我试图在数组内的每个帖子数据中放置一个 timeago,帖子数据来自 mysql 表。代码如下:

  $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;
  }

这些行已被放入一个数组中。每个里面都有:用户,帖子内容,时间(我用它来计算timegao)

timeago 脚本如下:

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";  
    }  

我如何遍历每个数组行,获取超时,使用 timeago 函数计算它,然后将结果作为 'timeago':'value-calculated' 注入每个数组行?

4

2 回答 2

0
$rows = array();
while($row = mysql_fetch_array( $query )) {
  $rows .= timeago($row['time']);
}
于 2012-07-06T06:25:54.163 回答
0

如下所示,PS:$counter不是必需的。

$rows = array();
while($row = mysql_fetch_array( $query )) {
  $row['timeago'] = timeago($row['time']);
  $rows[] = $row;
}
于 2012-07-06T06:13:29.257 回答