Php 需要检查用户是否在线的帮助吗
我对PHP和MySQL相当陌生,所以我需要一些帮助Php 需要检查用户是否在线的帮助吗,php,mysql,Php,Mysql,我对PHP和MySQL相当陌生,所以我需要一些帮助 <?php function Agotime($date) { if(empty($date)) { return "No date provided";
<?php
function Agotime($date)
{
if(empty($date)) {
return "No date provided";
}
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$unix_date = strtotime($date);
// check validity of date
if(empty($unix_date)) {
return "Unknown";
}
// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from now";
}
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] {$tense}";
}
$date = $run_user['lastloggedin'];
$result = Agotime($date); // 2 days ago
$serverjoins = $run_user['server_joins'];
?>
我有那个代码,它正常工作,但有一个问题,我的数据库中有另一行名为online,如果设置为true,我希望它在线显示,而不是上次看到的:1小时前
有人能告诉我把什么放在哪里吗?我假设$run\u user包含来自您数据库的结果,除了lastloggedin字段外,还有一个在线字段:
if ('true' == $run_user['online'] {
$result = 'Online now!';
} else {
$date = $run_user['lastloggedin'];
$result = Agotime($date);
}
不清楚。数据库结果在哪里?您如何将其与此功能结合?@Javad我不明白您的回答,您能再清楚一点吗?我的意思是
$date
是否来自数据库?您提供的此函数仅用于比较日期和时间您应该创建一个包含临时会话数据的表,并查询该表以检查某人是否在线,删除会话数据时删除该行如何定义“在线”?好的,我不知道为什么,但当我将数据库中的值从true更改为false时,它并没有改变结果,只是一直在说在线!对不起,应该是“真的”不是真的。看看这能不能解决问题。