0

所以,我有这个查询:

$alt_user_result = execute_pdo_query("SELECT U.username, U.user_id, T.last_activity_time
FROM user_skills S, users U
LEFT JOIN user_status T ON U.user_id=T.user_id
WHERE S.user_id=U.user_id and S.task_type_id=? and S.permission='Yes';",
array($tablerow['task_type_id']));

我正在做这样的事情来显示状态:

foreach($alt_user_result as $cur_user) {

 if($cur_user['user_id'] != 1){

   if( (time(date('Y-m-d H:i:s')) - time($cur_user['last_activity_time'])) < 300 ){
    echo "<option value='$cur_user[user_id]'>$cur_user[username] | online</option>";
   }
   else if( (time(date('Y-m-d H:i:s')) - time($cur_user['last_activity_time'])) >300){
    if( (time(date('Y-m-d H:i:s')) - time($cur_user['last_activity_time']))<1800 ) {
     echo "<option value='$cur_user[user_id]'>$cur_user[username] | idle</option>";
    }
   }
   else if( (time(date('Y-m-d H:i:s')) - time($cur_user['last_activity_time']))>1800 ){
     echo "<option value='$cur_user[user_id]'>$cur_user[username] | offline</option>";
   }
  }
  var_dump((time(date('Y-m-d H:i:s')) - time($cur_user['last_activity_time'])));
 }
 echo "<input type='submit' name='reassign' value='Reassign'/></select></form>";
}

last_activity_time我的表中是:datetime

表中的var_dump结果仅为零并且不正确,如果我删除time()它会显示时间之间的差异但time()它会中断;

如果最后一个活动是 <= 5 分钟 => 状态,我需要做的是:在线

如果它 > 5 分钟并且 <= 30 错误 => 状态:空闲

如果 > 30 分钟 => 状态:离线。

我做错了什么?有任何想法吗?

4

4 回答 4

2

你可以用 MySQL TIMESTAMPDIFF()CASE函数来解决它

SELECT * , TIMESTAMPDIFF( 
MINUTE , last_active, NOW( ) ) AS minuteDiff, 
CASE  
WHEN TIMESTAMPDIFF( MINUTE , last_active, NOW( ) ) >30 THEN  'Offline'
WHEN TIMESTAMPDIFF( MINUTE , last_active, NOW( ) ) > 4 THEN  'Idle'
WHEN TIMESTAMPDIFF( MINUTE , last_active, NOW( ) ) THEN  'Online'    
END AS userStatus
FROM tbl_time

现在检查userStatus列上的条件

更新

注意:您可以使用图像或任何您想要的东西,这并不重要。

经过上述查询后,您可以按以下方式将其显示在 php 中

<select name ="selectName" id ="selectID" > 
foreach($alt_user_result as $cu)
{

    switch($cu['userStatus'])
    {
        case 'Online' :
                        $txt = 'Online';
                        $img = 'images/online.png';
                        break;
        case 'Idle' :
                    $txt = 'Idle';
                    $img = 'images/idle.png';
                    break;
        case 'Offline' :
                    $txt = 'Offline';
                    $img = 'images/offline.png';
                    break;
        default : 
                // here you can set default value
    }

    if($cu['user_id'] != 1) {
      echo "<option value='$cu[user_id]'> $cu['username'] | $txt 
                /*  or you can show respective image */
                | <img src ='$img' alt='$txt' title='$txt'/>
            </option> ";
    } // end of if
} // end of foreach
</select>   
于 2012-08-09T13:24:13.333 回答
1

我已经给出了用户状态的 sql 查询。您可以在循环中直接使用该列。不需要if条件。

$alt_user_result = execute_pdo_query("
SELECT U.username, U.user_id, T.last_activity_time,
case 
WHEN TIMESTAMPDIFF( MINUTE, T.last_activity_time ,NOW() ) <= 5 THEN 'online'
WHEN ( TIMESTAMPDIFF( MINUTE, T.last_activity_time ,NOW() ) > 5 AND 
   TIMESTAMPDIFF( MINUTE, T.last_activity_time ,NOW() ) <=30 ) THEN 'idle'
ELSE 'offline' END as user_status   
FROM user_skills S, users U
LEFT JOIN user_status T ON U.user_id=T.user_id
WHERE S.user_id=U.user_id and S.task_type_id=? and S.permission='Yes';",
array($tablerow['task_type_id']));

IE

echo "<option value='$cur_user[user_id]'>$cur_user[username] | $cur_user[user_status]</option>";

如果您想将状态更改为可以管理的图像。

echo "<option value='$cur_user[user_id]'>$cur_user[username] | ";

if( $cur_user[user_status]=='online') echo "<img src='online.jpg'/>";
else if( $cur_user[user_status]=='idle') echo"<img src='idle.jpg'/>";
else if( $cur_user[user_status]=='offline') echo "<img src='offline.jpg'/>";

echo "</option>";
于 2012-08-09T13:22:38.820 回答
1

如果想获取两个日期之间的差异,请使用 php 的 dateTime。该函数返回两个日期之间的差值,无论结果是正数还是负数,它总是只返回差值。然后,您可以以任何您想要的格式输出它:

function date_diff($date1,$date2){
    try {
        $d1Object = new DateTime($date1);
        $d2Object = new DateTime($date2);

        $diff = $d1Object->diff($d2Object);
    }

    catch (Exception $e) {
        echo '';
    }
    //this returns the difference in years, e.g.
    //for more info, see php.net dateTime
    return $diff->y;
}
于 2012-08-09T21:18:54.523 回答
-1

我不写 PHP,但这些方面的东西应该让你开始:

    foreach($alt_user_result as $cur_user) 
    {
        if($cur_user['user_id'] != 1)
        {
            $interval = time(date('Y-m-d H:i:s'))->diff(time($cur_user['last_activity_time']));

            $numSeconds = $interval->s + $interval->m * 60 + $interval->h * 60 * 60 + $interval->d * 60 * 60 * 24;

            $status = "offline";

            if ($numSeconds < 300)
            {
                $status="online";
            }
            else if ($numSeconds < 1800)
            {
                $status="idle";
            }

            echo "<option value='$cur_user[user_id]'>$cur_user[username] | ".$status."</option>";
        }
    }
于 2012-08-09T12:59:45.480 回答