phpjquerymysqltimeago

"Time ago" from MySQL timestamp


I'm trying to make a table of results including the timestamp for each entry from the database. However, I need the time to be displayed as "X seconds/minutes/hours ago".

I've tried the jQuery timeago plugin with zero success and am now wondering how to do this for each entry.

$result = mysql_query("SELECT * from realmfeed order by ID asc");

echo "<table class='display'>\n";

while($row = mysql_fetch_array($result))
{
    echo "<tr><td><b>".$row['ID']."</b></td>\n";
    echo "<td>".$row['eventType']."</td>\n";
    echo "<td>".$row['server']."</td>\n";
    echo "<td>".$row['realm']."</td>\n";
    echo "<td>".$row['name']."</td>\n";
    echo "<td>".$row['time']."</td>\n</tr>\n\n";
}


echo "</table>\n";

How is it possible to create a "time ago" function for each result?


Solution

  • $time = strtotime($row['time']);
    $dbDate = new DateTime($time);
    $currDate = new DateTime();
    $interval = $currDate->diff($dbDate);
    echo $interval->d." days ".$interval->h." hours";
    

    please refer to DateInterval for available functions and fields