Subtracting time and whatnot.

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
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Subtracting time and whatnot.

Post by Mightywayne »

So I'm making a script to tell when the person's last action on the game was, and I don't want to just put it as "Last action was at blah blah.", I want to be like "It was 2 hours, 3 minutes ago." or something like.

Basically what will happen is their last action goes into the DB, then I use date(); to "subtract" the other time from now. Unfortunately I have no idea how to go about this, and the date(); talk on PHP website is confusing as usual.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Subtracting time and whatnot.

Post by Christopher »

Use Unix timestamps and subtract the current time from the previous. That will give you the elapsed time in seconds. Then divide by 60*60 for hours and the remainder by 60 for seconds.
(#10850)
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Re: Subtracting time and whatnot.

Post by Mightywayne »

Perfection. Thanks.
Post Reply