所以,我有这个问题:
$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()
它会中断; < / p>
如果最后一项活动是&lt; = 5分钟=&gt;,我需要做什么 IS 状态:在线
如果是&gt; 5分钟并且&lt; = 30 mis =&gt;状态:空闲
如果是&gt; 30分钟=&gt;状态:离线。
我做错了什么?任何想法?
答案 0 :(得分: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>
答案 1 :(得分:1)
我已经为用户状态提供了sql查询。您可以在循环中直接使用该列。不需要条件。
$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']));
即。
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>";
答案 2 :(得分: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;
}
答案 3 :(得分:-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>";
}
}