我正面临一个挑战,我试图添加用户在网上花费的所有时间,但总计却出现了错误。我从mysql数据库中获取值,我想获取每个用户的总时,分和秒。目前,我正在获取每个会话的总计,但是现在我想要所有会话的总计,并在一个表中显示所有用户,在该表中我具有用户名和在线花费的时间,并且必须能够按日期过滤结果以获取花费的总时间(以小时,分钟和秒为单位)过滤日期。我怎样才能做到这一点。我的代码如下:
$i=0;
foreach ($report as $online)
{
$id = $online['userid'];
$sql = "SELECT start_time, end_time FROM user_sessions where userid=$id AND end_time !='0000-00-00' || end_time != '' LIMIT $start, $per_page";
$timeOnline = $obj->MySQLSelect($sql);
++$i;
$start = $timeOnline[$i]['start_time'];
$end = $timeOnline[$i]['end_time'];
$totalhours = get_saved_time($end, $dstart);
$sum = $totalhours;
?>
<tr class="gradeA">
<?php if (!empty($sum)): ?>
<td><? echo $generalobjAdmin->clearName($online['fname'].' '.$online['lname']); ?></td>
<td align="center"><?= Convert($sum); ?></td>
<?php endif; ?>
</tr>
<? } ?>
public function get_saved_time($end_time,$start_time)
{
$past = $start_time;
$current = strtotime($end_time);
$past = strtotime($past);
return round(abs($current-$past));
}
function Convert($seconds) {
$hours = floor($seconds / 3600);
$mins = floor(($seconds / 60) % 60);
$secs = $seconds % 60;
if (strlen($hours) == 1)
$hours = "0" . $hours;
if (strlen($secs) == 1)
$seconds = "0" . $secs;
if (strlen($mins) == 1)
$minutes = "0" . $mins;
if ($hours == 0){
$mint="";
$secondss="";
if($mins > 01){
$mint = "$mins mins";
}else{
$mint = "$mins min";
}
if($secs > 01){
$secondss = "$secs seconds";
}else{
$secondss = "$secs second";
}
$ret = "$mint $secondss";
} else {
$mint="";
$secondss="";
if($mins > 01){
$mint = "$mins mins";
}else{
$mint = "$mins min";
}
if($secs > 01){
$secondss = "$secs seconds";
}else{
$secondss = "$secs second";
}
if($hours > 01){
$ret = "$hours hrs $mint $secondss";
}else{
$ret = "$hours hr $mint $secondss";
}
}
return $ret;
}