Isomerizer Posted August 14, 2008 Share Posted August 14, 2008 Ok, Time stamps have been confusing quite a few recently... So heres a small but may come in handy function that will calculate the last time the user was seen. function lastaction($time) { // $time = the lastaction fetched from users table // if (!preg_match("[^0-9]", $time)) { die ('Time invalid');} // make sure the $time is actually numeric $last = time() - $time; $unit = 'seconds'; // $default unit if ($last >= 60) { $last = $last / 60; $unit = 'minutes'; } // >= 60, divide by 60 if ($last >= 60) { $last = $last / 60; $unit = 'hours'; } // >= 60, divide by 60 again if ($last >= 24) { $last = $last / 24; $unit = 'days'; } // >= 24, divide by 24 $lastaction = sprintf("%u %s", $last, $unit); return $lastaction; // return the result.. } // ex: $last = lastaction($fetch['lastaction']); echo $last; result = last time player made an action Quote Link to comment Share on other sites More sharing options...
Floydian Posted August 14, 2008 Share Posted August 14, 2008 Re: Simple Lastaction Func CRAP!!! Ok, Time stamps have been confusing quite a few recently... Including Isomerizer!! The regex is a waste of time. Are we really going to take the extraordinary action of killing script execution if a number isn't passed to the function? Wow... Second, minutes, hours, and days will always be plural. That's a small oversight, but when I see 1 days, or 1 hours, or 1 minutes on a game, it makes me think they're lazy folks. Worst of all is the logic error. You're performing all of your manipulations on the time in the $last variable. But you are returning the $time variable!!! This function doesn't do a damn thing!! Here is the cleaned up function: function lastaction($time) { // $time = the lastaction fetched from users table settype($time, 'integer'); // make sure the $time is actually numeric $last = time() - $time; if ($last >= 24 * 60 * 60) { $last = floor($last / (24 * 60 * 60)); $unit = $last == 1 ? 'day' : 'days'; } // check days elseif ($last >= 60 * 60) { $last = floor($last / (60 * 60)); $unit = $last == 1 ? 'hour' : 'hours'; } // check hours elseif ($last >= 60) { $last = floor($last / 60); $unit = $last == 1 ? 'minute' : 'minutes'; } // check minutes else { $unit = $last == 1 ? 'second' : 'seconds'; } // lastly check seconds $lastaction = sprintf("%u %s", $last, $unit); return $lastaction; // return the result.. } // ex: $last = lastaction($fetch['lastaction']); echo $last; result = last time player made an action I tested this function and it operates as required. Quote Link to comment Share on other sites More sharing options...
Isomerizer Posted August 14, 2008 Author Share Posted August 14, 2008 Re: Simple Lastaction Func Bah, my bad, Only just realised i was returning the wrong thing lol. Thanks for correcting the function though, also learnt that floor() &/ ceil() can come in handy. :P Quote Link to comment Share on other sites More sharing options...
Floydian Posted August 14, 2008 Share Posted August 14, 2008 Re: Simple Lastaction Func I was originally going to say it was a nice function, but then I saw the return error during testing. But it is a nice function that you mostly came up with. ;) Kudos 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.