tags:

views:

41

answers:

2

I am trying to work out if users have had activity in the past hour. The following is my code, but it always returns yes, no matter what:

$houraway = (int)mktime() + (60 * 60);
$now = mktime();

while($user = mysql_fetch_array($qusers))   {
    echo $user['Name']." - ";
    $datetime = explode(" ", $user['Custom6']);
    $date = $datetime[0];
    $date = explode("-", $date);
    $time = $datetime[1];
    $time = explode(":", $time);
    $hr = $time[0];
    $min = $time[1];
    $sec = $time[2];
    $y = $date[0];
    $m = $date[1];
    $d = $date[2];
    $lastloginmk = (int)mktime($hr, $min, $sec, $m, $d, $y);
    echo " - ".$lastloginmk;
    if($lastloginmk <= $houraway) { echo "yes"; } else { echo "no"; }
    echo "<br />";
}

last activity time is stored in the database like this:

2009-09-22 13:32:28

Thanks in advance!

Ryan

+1  A: 

The line

$houraway = (int)mktime() + (60 * 60);

is looking into the future by an hour. The last login will always be earlier than the future, because the last login must be in the past.

I think you would want last login to be greater than one hour before now. Change the $houraway and change your last comparison, and you should be good to go.

Mark Rushakoff
oh how stupid of me. thanks heaps. good pick up.
Ryan
+1  A: 

I think the code you want is:

$hourago = mktime() - (60 * 60);

while($user = mysql_fetch_array($qusers))   {
    echo $user['Name']." - ";
    $lastloginmk = strtotime($user['Custom6']);
    echo " - ".$lastloginmk;
    if($lastloginmk <= $hourago) { echo "yes"; } else { echo "no"; }
    echo "<br />";
}

Hope this helps.

NawaMan