I'm creating comments stored in a MySQL database.
I'm logging the php time function time()
as the comment is posted. That way it's displaying a message such as... "comment... posted 4 seconds ago" and if I refresh the page 2 minutes later it'd display "comment... posted 2 minutes ago"
Here's how I am entering time()
into the database along with the other data:
$date=time();
// Insert data into mysql
$sql="INSERT INTO testimonials (username, comment, date)
VALUES ('$username', '$comment', '$date')";
Now... I grab the data like this:
while ($row = mysql_fetch_row($result) )
{
echo "<b>Random Comment</b></br>";
echo ("<p>\n> $row[1]"); //comment
echo ("</br>-$row[0]</p>"); //name
echo ("</br>$row[2]"); //date
The sample output on my server is:
Random Comment
This is the most awesome comment thing ever!!!!
-Kyle
1278905319
How could I convert the time "1278905319" into a readable format such as, "posted 4 seconds ago" or something that deals with seconds, minutes, hours, days, weeks, months, years?
Is PHP the wrong way to do it? I read about MySQL timestamping but I don't understand that or how to make it work in my case.
So my main question is, how to format the time into a readable time on output. "2 seconds ago"
Thank you. =)