[英]How to find total work hours for certain period of time using PHP
I have a simple table query like bellow: 我有一个像波纹管这样的简单表查询:
SELECT uid, dates, time_in, time_out,
TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(time_out)-TIME_TO_SEC(time_in))),'%H:%i') as
total_wh FROM tb_attendance WHERE uid=10012 AND dates BETWEEN '2017-02-01' AND
'2017-02-07' GROUP BY dates
That will output table like bellow: 这样将输出如下表:
uid dates time_in time_out work_hours
10012 2017-02-01 07:07:02 15:56:48 08:49
10012 2017-02-02 07:13:13 07:13:13 00:00
10012 2017-02-03 06:55:35 06:55:35 00:00
10012 2017-02-06 06:45:50 06:45:50 00:00
10012 2017-02-07 06:53:19 16:06:36 09:13
Grand Total ?????
I need to find grand total for work hours in question marks using PHP. 我需要使用PHP查找问号总计的工作时间。 So far I've been trying these codes but it returns incorrect value. 到目前为止,我一直在尝试这些代码,但返回的值不正确。
`echo"<table><tr><td>uid</td><td>dates</td><td>time_in</td><td>time_out</td><td>work_hours</td></tr>";
$totalwh=0;
while($row=mysql_fetch_array($sql)){
echo"<tr><td>".$row['uid']."</td>";
echo"<td>".$row['dates']."</td>";
echo"<td>".$row['time_in']."</td>";
echo"<td>".$row['time_out']."</td>";
echo"<td>".$row['total_wh']."</td>";
echo"</tr>";
$workhours=explode(":",$row['total_wh']);
$totalwh+=$workhours[0]*60;
$totalwh+=$workhours[1];
$hours = $totalwh / 60;
$minutes = $totalwh % 60;
}
$x=date('H:i',strtotime("$hours:$minutes"));
echo"<tr><td colspan=4 align='center'>Grand Total </td><td>".$x."</td></tr>
</table>";
`
Please help! 请帮忙!
I would recommend calculating the total time in seconds, then formatting the seconds into a date representation string. 我建议以秒为单位计算总时间,然后将秒格式设置为日期表示形式的字符串。 This formatting can be done with the functon gmdate() 可以使用functon gmdate()完成此格式化
http://php.net/manual/en/function.gmdate.php http://php.net/manual/zh/function.gmdate.php
The time in seconds could either be fetched directly from the database using one of the query functions or the following could be done. 可以使用一种查询功能直接从数据库中获取以秒为单位的时间,或者可以执行以下操作。
<?php
//Store total time in seconds
$totalTimeSeconds = 0;
//Loop over rows
while($row=mysql_fetch_array($sql)){
//Output row
//Split total working hours into hours and minutes
$time = explode(":", $row['total_wh']);
$timeHours = $time[0];
$timeMinutes = $time[1];
//Convert and add hours/minutes to total time
$totalTimeSeconds += ($timeHours * 60 * 60);
$totalTimeSeconds += ($timeMinutes * 60);
}
//Output formatted total time in seconds
gmdate("H:i:s", $totalTimeSeconds);
Hope this helps! 希望这可以帮助!
Try this 尝试这个
SELECT uid, dates, time_in, time_out,SEC_TO_TIME(SUM(TIME_TO_SEC(time_out)-TIME_TO_SEC(time_in))) as
total_wh FROM tb_attendance WHERE uid=10012 AND dates BETWEEN '2017-02-01' AND '2017-02-07' GROUP BY dates
Just replace 只需更换
TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(time_out)-TIME_TO_SEC(time_in))),'%H:%i') as total_wh
with 与
SEC_TO_TIME(SUM(TIME_TO_SEC(time_out)-TIME_TO_SEC(time_in))) as total_wh
It gives output as 它给出的输出为
You Change your code like this 您可以像这样更改代码
<?php
echo "<table>
<tr><td>uid</td>
<td>dates</td>
<td>time_in</td>
<td>time_out</td>
<td>work_hours</td></tr>";
$totalwh=0;
while($row=mysql_fetch_array($sql)){
echo"<tr><td>".$row['uid']."</td>";
echo"<td>".$row['dates']."</td>";
echo"<td>".$row['time_in']."</td>";
echo"<td>".$row['time_out']."</td>";
echo"<td>".$row['total_wh']."</td>";
echo"</tr>";
$time_arr[] = $row['total_wh'];
}
function explode_time($time) { //explode time and convert into seconds
$time = explode(':', $time);
$time = $time[0] * 3600 + $time[1] * 60;
return $time;
}
function second_to_hhmm($time) { //convert seconds to hh:mm
$hour = floor($time / 3600);
$minute = strval(floor(($time % 3600) / 60));
if ($minute == 0) {
$minute = "00";
} else {
$minute = $minute;
}
$time = $hour . ":" . $minute;
return $time;
}
$time = 0;
foreach ($time_arr as $time_val) {
$time +=explode_time($time_val); // this fucntion will convert all hh:mm to seconds
}
echo"<tr><td colspan=4 align='center'>Grand Total </td><td>".second_to_hhmm($time)."</td></tr>
</table>";
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.