Using PHP/mySQL, a user is granted a single integer point to their member account each day. The data I will use to determine if a point should be granted are these mysql fields: Creation Date (timestamp) and Last Login (UNIX TIME).
The procedure for granting these points is determined when the user logs in. My question is, what's the most efficient way of determining how many days have passed since the last login? Secondly, if a user logs in each day, how do I determine if 24 hours has passed and a point is to be granted? Days past equates to the points given (1 per day).
Currently I am using this code:
/*
** Updates Points based on days since last visit
*/
static function UpdatePoints($username)
{
$getlog = System::$mySQL->Select("lastLog, creation FROM users WHERE username='$username'");
$log = System::$mySQL->Fetch($getlog);
$offset = (TIME() - $log['lastLog']) / 86400; // 24hrs
$lastlog = round($offset); // in days
if($lastlog > 0)
{
System::$mySQL->Update("users SET points=points+".$lastlog." WHERE username='$username'");
}
}
Markup aside, it's obvious my code is shortsighted. If the user logs in once everyday, they do not gain a point. Therefore I must determine the correct method for doing so using the Creation Date field as well. I just can't wrap my head around it today, any suggestions? Thanks.