MakeWebGames

Time :s

Recommended Posts

A couple of weeks ago, i was asked to add to the usersonline.php how long people had been online for...

i attempted to do this, but felt it was slightly out... now some people are telling me it doubles how long they have been online for...

I just wondered what the easiest way to do this was.... i currently take a unix timestamp when they log in, and use the laston timestamp already taken

```\$online=\$r['laston']-\$r['lastlogin'];
\$onunits="secs";

if (\$online<0){\$online='0';}
if (\$online>=60){\$onunits="mins"; \$online=(int)(\$online/60);}
if (\$online>=24){\$onunits="hrs"; \$online=(int)(\$online/24);}

\$online="\$online\$onunits";```

can anyone help me spot where the error is?

Share on other sites

Re: Time :s

`if (\$online>=60*60){\$onunits="hrs"; \$online=(int)(\$online/(60*60));}`

The math on the hours was wrong. You were dividing seconds by 24. That gives you a unit of slightly less than half a minute (half a minute = seconds / 30)

So, for hours, it's 60*60 and then divide the seconds by that ;)

Share on other sites

Re: Time :s

That gets the time right, but keeps it in minutes, instead of saying 1hr it says 78 mins

Share on other sites

Re: Time :s

That would be due to a seperate error which is the order of your if's and the fact that you'll need elseif's instead of just plain if's.

```if (\$online<0){\$online='0';}
elseif (\$online>=60*60){\$onunits="hrs"; \$online=(int)(\$online/(60*60));}
elseif (\$online>=60){\$onunits="mins"; \$online=(int)(\$online/60);}
else { // I guess this would be for seconds.....
}```
Share on other sites

Re: Time :s

i think this has worked, and thanks for your help...

Time will tell :D

Share on other sites

Re: Time :s

You're welcome ;)

You weren't far off there and that stuff trips up everyone lol (including me in the past.)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.