backwords counting

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
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

backwords counting

Post by pinehead18 »

Ok, lets pretend that i have a lil forum on my website. (i really do) and whenever someone posts a new topic or reply mysql creates a time stamp that, well lets say looks like this "1092791237" and i want to write a little script that says how many min or hour ago that the reply or new topic was posted.

How might i go about that?

Thank you
Anthony
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post by litebearer »

Since a timestamp is seconds,
[QUOTE}
seconds_elapsed = timestamp_now - timestamp_then
minutes_elapsed = floor(seconds_elapsed / 60)
[/QUOTE]
and so on

Lite...
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

Now what if it has been more than 60 min and i want to say 1 hour 15 min.

How would i go about that calculation?

Thank you for your help.

Anthony
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

his "code" says minutes elapsed.. not hours..
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

Right, i was just wondering if i could use the minutes elapsed and create hours and minutes. LIke 5 hours 4 min ago
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can, however, you will need additional math for each component then.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post by litebearer »

this is simply the thought process (using MODULUS etc will shorten the coding)

(read psuedo code)

Code: Select all

hours = floor(seconds_elasped / (60*60))
    seconds_left_over = seconds_elapsed - (hours*60*60)
minutes = floor(seconds_left_over/60)
seconds = seconds_left_over - (minutes*60)
elapsed_time = hours:minutes:seconds
Post Reply