I use the same code to show how long a user has been away from the site on 2 servers. On one server it executes the code correctly, and if a user is online, it shows that a user has been online less than 1 second ago (>1 sec ago). On the other server, it shows the user has been idle for 6 days, 17 hours, ago.
Here is the code I use
$result = mysql_query('SELECT * FROM users ORDER BY last_login DESC LIMIT 5');
while ($row = mysql_fetch_assoc($result))
{
$last_login = $row['last_login'];
$diff = time('g:i:s A') - strtotime($last_login);
if (duration($diff,'long') ==True) {
echo "<b>".$row['username'] . '</b>: ' . duration($diff,'long') . ' ago<br>';
}
else {
echo "<b>".$row['username'] . "</b>: >1 second ago<br>";
}
}
Here is the function called "duration".
function duration ($seconds, $suffix=FALSE) {
$takes_time = array(604800,86400,3600,60,0);
$suffixes = array("week","day","hour","minute","second");
$delimeter = array(" W ", " D ", ":",":","");
$output = "";
foreach ($takes_time as $key=>$val) {
${$suffixes[$key]} = ($val == 0) ? $seconds : floor(($seconds/$val));
$seconds -= ${$suffixes[$key]} * $val;
if (${$suffixes[$key]} > 0 || (!empty($output) && $suffix == FALSE)) {
if ($val == 0 && $suffix == FALSE && empty($output)) {
$output .= "00:";
}
$output .= ($key > 1 && strlen(${$suffixes[$key]}) == 1 && $suffix == FALSE) ? "0".${$suffixes[$key]} : ${$suffixes[$key]};
if ($suffix == "short") {
$output .= substr($suffixes[$key],0,1)." ";
}
elseif ($suffix == "long") {
$output .= (${$suffixes[$key]} > 1) ? " ".$suffixes[$key]."s, " : " ".$suffixes[$key].", ";
}
else {
$output .= $delimeter[$key];
}
}
}
return $output;
}
Can it be caused by the server or is there an error in the code that one server happens to overlook? Can it be a database issue? All dates in the database are in DATETIME format.
Also, both servers seem to be running PHP 4.3.10.