如何在我的数据表中实现Timeago函数?

时间:2017-03-25 07:19:00

标签: javascript php mysql datatables timestamp

我先说这是一个初学者,所以请轻松一点。 我目前正在使用datatables插件从Mysql输出一些行。这包括一个时间戳,我想将其转换为“以前”格式。我研究过并发现有多个js脚本执行此操作:moments.js,timeago.js,livestamp.js。我试过没有成功,这是我的代码:

<table id="player_data" class="table table-bordered">
        <thead>
            <tr>
                <td>1</td>
                <td>2</td>
                <td>3</td>
                <td>4</td>
                <td>5</td>
                <td>Time</td>
            </tr>
        </thead>
         <?php
          while($row = mysqli_fetch_array($result))  
          {  
               echo '  
               <tr>  
                    <td>'.$row[ "1"]. '</td>  
                    <td>'.$row[ "2"]. '</td>  
                    <td>'.$row[ "3"]. '</td>  
                    <td>'.$row[ "4"]. '</td>  
                    <td>'.$row[ "5"]. '</td>  
                    <td>'.$row[ "time"]. '</td>
               </tr>  
               ';  
          }  
          ?>
</table>

如何以x秒前的格式回显“时间”?

1 个答案:

答案 0 :(得分:0)

尝试以下代码

 $time = strtotime($time);

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':'');
    }

}

echo humanTiming($time).' ago';

希望它有所帮助!