在mysqli_fetch_array中将函数的结果插入值中

时间:2017-01-03 09:37:30

标签: php

我有一个计算2个日期之间差异的函数。 它给出了这样的结果:事件发生在6年前

如何设置此函数,以便每次从列中获取日期并在变量timedif内向我们提供结果(字符串):

.....
while($row = mysqli_fetch_array($result)){
 array_push($res, array(
 "postText"=>$row['postText'],
 "location"=>$row['location'],
 "timedif"=>humanTiming($row['time'])));     
 }

 //Displaying the array in json format 
 echo json_encode($res);
 }else{
            echo "over";
    }

上面的代码不起作用

humanTiming功能:

function humanTiming ($time)
{

    $time = time() - $time; // to get the time since that moment
    $time = ($time<1)? 1 : $time;
    $tokens = array (
        31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
    );

    foreach ($tokens as $unit => $text) {
        if ($time < $unit) continue;
        $numberOfUnits = floor($time / $unit);
        return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
    }

}

0 个答案:

没有答案