2017-03-25 25 views
1

讓我通過說我是初學者,這麼簡單。 我目前使用數據表插件從Mysql輸出一些行。這包括一個時間戳,我想將它轉換爲「以前」的格式。我研究並發現有多個js腳本可以實現這一點:moments.js,timeago.js,livestamp.js。我已經沒有成功嘗試,這裏是我的代碼:如何在數據表中實現Timeago功能?

<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秒前格式回聲「時間」?

+0

什麼是輸出和預期的結果? – C2486

+1

[在PHP中轉換timestamp到時間的前例如1天前,2天前...](http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php - 例如,1天前,2天前) – Jigar

回答

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

希望它能幫助!

相關問題