dousuitang5239 2017-03-25 07:18
浏览 45

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

Let me preface this by saying I'm a beginner, so go easy. I'm currently using the datatables plugin to output some rows from Mysql. This includes a Timestamp, and I would like to convert it into an "ago" format. I've researched and found that there are multiple js scripts that do this : moments.js, timeago.js, livestamp.js. I have tried with no success here is my code :

<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>

How can I echo "time" in a x seconds ago format?

  • 写回答

1条回答 默认 最新

  • dtr87341 2017-03-25 08:10
    关注

    Try following code

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

    Hope it helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目