Jump to content
MakeWebGames

Simple Lastaction Func


Isomerizer

Recommended Posts

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 
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...