需要帮助来检查用户是否在线

时间:2014-03-13 20:30:32

标签: php mysql

我是PHP和MySQL的新手,所以我需要一些帮助。

                        <?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'];
                        ?>

我有那个代码,并且它应该正常工作,但是有一个问题,我在我的数据库中有另一行称为在线,如果这被设置为true,我希望它显示在线而不是例如最后看到:1小时前 谁能告诉我在哪里放什么?

1 个答案:

答案 0 :(得分:1)

我假设$ run_user包含数据库的结果,除了lastloggedin字段外还有一个在线字段:

if ('true' == $run_user['online'] {
  $result = 'Online now!';
} else {
  $date = $run_user['lastloggedin'];
  $result = Agotime($date);
}