echo "<table><tr>"; $sql = "SELECT distinct uid, uname, user_avatar, last_login FROM ".$xoopsDB->prefix("users")." WHERE level > 0 AND uid NOT IN (0) AND last_login >= '" . $time . "' ORDER BY last_login DESC"; $result = $xoopsDB->query($sql);
I'm assuming $last_login is a timestamp column in the database.
PHP Code:
<?php /*************************************************************************************************** * Copyright (C) 2009 JCBones. * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see <http://www.gnu.org/licenses/>. * * FUNCTION TIMEDIFFERENCE. * * set real dates for start and end, otherwise *nix the strtotime() lines. * * $return 'days' will return days/hours/minutes/seconds. * * $return 'hours' will return hours/minutes/seconds. * * $return 'minutes' will return minutes/seconds. * * $return 'seconds' will return seconds. * * * ***************************************************************************************************/ function timeDifference($start,$end,$return='days') { //change times to Unix timestamp. //$start = strtotime($start); //$end = strtotime($end); //subtract dates $difference = max($end, $start) - min($end,$start); $time = NULL; //calculate time difference. switch($return) { case 'days': $days = floor($difference/86400); $difference = $difference % 86400; $time .= $days . ' Days, '; case 'hours': $hours = floor($difference/3600); $difference = $difference % 3600; $time .= $hours . ' Hours, '; case 'minutes': $minutes = floor($difference/60); $difference = $difference % 60; $time .= $minutes . ' Minutes, '; case 'seconds': $seconds = $difference; $time .= $seconds . ' Seconds'; }
echo "<table><tr>"; $sql = "SELECT distinct uid, uname, user_avatar, last_login FROM ".$xoopsDB->prefix("users")." WHERE level > 0 AND uid NOT IN (0) AND last_login >= '" . $time . "' ORDER BY last_login DESC"; $result = $xoopsDB->query($sql);
while (list($uid, $uname,$user_avatar,$poster,$last_login) = $xoopsDB->fetchRow($result) ) { //Put this variable in the echo statements where you would like them to show up. $sinceLastLogin = 'Last Login was: ' . timeDifference($last_login,$now,'days') . ' ago.'; $kira++; if ($user_avatar == 'blank.gif') { echo "<td style='padding:5px;text-align:center'><a href='".XOOPS_URL."/userinfo.php?uid=".$uid."'><img src='".XOOPS_URL."/uploads/blank_avatar.gif' title='".$uname."' border='0' alt='".$uname."' height='48' width='48'></a><br /><a href='".XOOPS_URL."/userinfo.php?uid=".$uid."'>".$uname."</a></td>"; }
actually what i wanted is the last login time is updated from seconds to minutes to hours etc ...not display all in one line
??? Not sure what you mean by this.
I figured that your database was holding that value, it is a unix timestamp (seconds since Jan 01, 1970). I fixed the function to return time values based on that.
So, I'll explain the code for you.
time(); is a php function that returns a unix timestamp.
$last_login from your database also holds a unix timestamp.
time() - $last_login will give you the number of seconds from your last login, to this very moment.
Subtract 86400 seconds per day, 3600 seconds per hour, 60 seconds per minute, and your left with the remaining seconds. That is all that this function does.
If you will give me the layout you would like to accomplish, I will help you to get it integrated.
echo "<table><tr>"; $sql = "SELECT distinct uid, uname, user_avatar, last_login FROM ".$xoopsDB->prefix("users")." WHERE level > 0 AND uid NOT IN (0) AND last_login >= '" . $time . "' ORDER BY last_login DESC"; $result = $xoopsDB->query($sql);
while (list($uid, $uname,$user_avatar,$poster,$last_login) = $xoopsDB->fetchRow($result) ) { //Put this variable in the echo statements where you would like them to show up. $sinceLastLogin = '[ ' . timeDifference($last_login,$now,'hours') . ' earlier ]'; $kira++; if ($user_avatar == 'blank.gif') { echo "<td style='padding:5px;text-align:center'><a href='".XOOPS_URL."/userinfo.php?uid=".$uid."'><img src='".XOOPS_URL."/uploads/blank_avatar.gif' title='".$uname."' border='0' alt='".$uname."' height='48' width='48'></a><br /><a href='".XOOPS_URL."/userinfo.php?uid=".$uid."'>".$uname."</a>$sinceLastLogin</td>"; }
echo "<table><tr>"; $sql = "SELECT distinct uid, uname, user_avatar, last_login FROM ".$xoopsDB->prefix("users")." WHERE level > 0 AND uid NOT IN (0) AND last_login >= '" . $time . "' ORDER BY last_login DESC"; $result = $xoopsDB->query($sql);
while (list($uid, $uname,$user_avatar,$poster,$last_login) = $xoopsDB->fetchRow($result) ) { //Put this variable in the echo statements where you would like them to show up. $sinceLastLogin = '[ ' . timeDifference($last_login,$now,'hours') . ' earlier ]'; $kira++; if ($user_avatar == 'blank.gif') { echo "<td style='padding:5px;text-align:center'><a href='".XOOPS_URL."/userinfo.php?uid=".$uid."'><img src='".XOOPS_URL."/uploads/blank_avatar.gif' title='".$uname."' border='0' alt='".$uname."' height='48' width='48'></a><br /><a href='".XOOPS_URL."/userinfo.php?uid=".$uid."'>".$uname."</a>$sinceLastLogin</td>"; }
Does not match, you have an extra variable there "$poster", which is probably holding your $last_login value. To test this, we should echo that out. Try this script here.
echo "<table><tr>"; $sql = "SELECT distinct uid, uname, user_avatar, last_login FROM ".$xoopsDB->prefix("users")." WHERE level > 0 AND uid NOT IN (0) AND last_login >= '" . $time . "' ORDER BY last_login DESC"; $result = $xoopsDB->query($sql);
while (list($uid, $uname,$user_avatar,$poster,$last_login) = $xoopsDB->fetchRow($result) ) { //Put this variable in the echo statements where you would like them to show up. $sinceLastLogin = '[ ' . timeDifference($poster,$now,'hours') . ' earlier ]'; $kira++; if ($user_avatar == 'blank.gif') { echo "<td style='padding:5px;text-align:center'><a href='".XOOPS_URL."/userinfo.php?uid=".$uid."'><img src='".XOOPS_URL."/uploads/blank_avatar.gif' title='".$uname."' border='0' alt='".$uname."' height='48' width='48'></a><br /><a href='".XOOPS_URL."/userinfo.php?uid=".$uid."'>".$uname."</a>$sinceLastLogin</td>"; }