2

so I have a table which lists chat messages in my game server (chat messages are stored in a database) and I have a UNIX timestap like this (for example) 1455749769

Does anyone know how I can use php to convert the timestamp so it echos how long ago the chat message was for example: "5 Seconds Ago"

Here is my table

$name=$row['client_name'];
$time=$row['msg_time'];
$name=htmlentities($name);
        echo "<tr>";
 echo "<td> $time </td>";
 echo "<td><a href='http://144.76.158.173/ech/user.php?id=".$row["client_id"]."' > $name </a></td>";
 echo "<td> $msg </td>";
 echo "</tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
echo "</div>";
}
$conn->close();
?>

Any help greatly appreciated :)

2 Answers2

2

The timestamp is seconds since the Epoch, so just get the current timestamp and subtract:

$seconds = time() - $time
Troels Larsen
  • 4,462
  • 2
  • 34
  • 54
  • Thank you, Second question, do you know how I can get it to do like 1m 5s ago and 1h 1m 5s ago and 1d 1h 1m 5s ago ? :) – Jonny Mackell Feb 17 '16 at 23:18
  • @JonnyMackell: Take a look at this answer: http://stackoverflow.com/questions/8273804/convert-seconds-into-days-hours-minutes-and-seconds – Troels Larsen Feb 17 '16 at 23:23
0

Just get the current time and substract:

$now = time();
//results into an unix like 1455750460
//then just substract:

$diff = $now - $time
// gives you the passed seconds

//readable
echo date('H:i:s', $diff);
Nicensin
  • 943
  • 1
  • 6
  • 15
  • Thank you, Second question, do you know how I can get it to do like 1m 5s ago and 1h 1m 5s ago and 1d 1h 1m 5s ago ? :) – Jonny Mackell Feb 17 '16 at 23:16
  • i edited the example above. have a look here: http://stackoverflow.com/questions/20258294/how-to-create-human-readable-time-stamp – Nicensin Feb 17 '16 at 23:27