I want to show when the comment last posted in PHP. like 5 minutes ago, 2 days ago, 7 weeks ago. How to do this?
-
Can you be more specific about what content management system you are using? Or if it is homebrewed, can you post the php that allows comments on your site? – Jon F Hancock Jan 16 '10 at 07:32
-
Generally this would be done in Javascript, as then you can use server-side caching. However, I can't seem to find the link. – Tyler Carter Jan 16 '10 at 07:33
-
In what format is the time/date that you want to convert? – Matthew Iselin Jan 16 '10 at 07:35
-
Is this question about how to generate fuzzy time, or how to print it? – Ryann Graham Jan 16 '10 at 07:35
-
I want to compare the updated_date which I get through a comments table and in PHP code I want to compare this updated_date (2010-01-15 16:46:43) with the current date time and show how many time before the comment posted. Like on stack overflow you get about how many time before the question is posted (3 mins ago). – snew Jan 16 '10 at 07:36
4 Answers
You can use timeago, a jQuery plugin, to do it via Javascript. It would yield the same result, update without refreshing, and by doing it client side instead of server side, you are not precluded from caching.
Otherwise, Annurag has a link with some good PHP solutions.

- 60,743
- 20
- 130
- 150
You can do manual calculation in server, to get the time difference, then translate it into human time format.
Or my preference, do it in browser using javascript. Using this approach, the time in page can be updated without refresing the page.
You can use this jQuery EasyDate library to translate a DOM element into humane time format.
You can also read the comments in this post about Pretty Date by John Resig. It contain the code, and improvement by other.

- 5,260
- 5
- 35
- 52
Store the comment posted in the date DB and show the the same in the front end by comparing with current date and time using php function

- 11
- 1