MobTown Posted September 11, 2008 Share Posted September 11, 2008 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? Quote Link to comment Share on other sites More sharing options...
Floydian Posted September 11, 2008 Share Posted September 11, 2008 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 ;) Quote Link to comment Share on other sites More sharing options...
MobTown Posted September 11, 2008 Author Share Posted September 11, 2008 Re: Time :s That gets the time right, but keeps it in minutes, instead of saying 1hr it says 78 mins Quote Link to comment Share on other sites More sharing options...
Floydian Posted September 11, 2008 Share Posted September 11, 2008 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..... } Quote Link to comment Share on other sites More sharing options...
MobTown Posted September 12, 2008 Author Share Posted September 12, 2008 Re: Time :s i think this has worked, and thanks for your help... Time will tell :D Quote Link to comment Share on other sites More sharing options...
Floydian Posted September 12, 2008 Share Posted September 12, 2008 Re: Time :s You're welcome ;) You weren't far off there and that stuff trips up everyone lol (including me in the past.) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.