Show elapsed time since record created?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rcoy
Forum Newbie
Posts: 1
Joined: Fri Apr 25, 2008 10:29 am

Show elapsed time since record created?

Post by rcoy »

I'm having a difficult time finding how to do this. I have people posting messages on my site and instead of showing the date they posted I would like to show how many minutes, or hours, or days since they posted. I've seen this on sites like twitter. I'm using PHP 5 and a MySQL database and new records are current time stamped. Thanks in advance for any help you can offer.

R. Coy
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Show elapsed time since record created?

Post by aceconcepts »

Use strtotime() to compare the time difference between the two times (dates).
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Show elapsed time since record created?

Post by onion2k »

In your SQL where you fetch the record put in something like..

Code: Select all

NOW() - `myTimestampField` AS seconds_since_posting
That'll give you a column called 'seconds_since_posting' that contains, well, the number of seconds since the post was posted. Then you just need to convert that into minutes, hours, days, etc. That's just a bit of division.
Post Reply