1

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?

magepvp117
  • 11
  • 1
  • what is output and what is expected ? – Niklesh Raut Mar 25 '17 at 07:23
  • 1
    Possible duplicate of [Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...](http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago) – Jigar Mar 25 '17 at 07:24

1 Answers1

0

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!

Raman
  • 624
  • 5
  • 13