如何使用PHP查找特定时间段的总工作时间

时间:2017-07-11 06:44:13

标签: php mysql sql database datetime

我有一个像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

那将输出如下表:

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                ?????

我需要使用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>";

$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>";
`

请帮忙!

3 个答案:

答案 0 :(得分:0)

我建议以秒为单位计算总时间,然后将秒格式化为日期表示字符串。可以使用功能 gmdate()

完成此格式设置

http://php.net/manual/en/function.gmdate.php

可以使用其中一个查询函数直接从数据库中获取以秒为单位的时间,也可以执行以下操作。

driver.save_screenshot('/screenshots/123.png')

希望这有帮助!

答案 1 :(得分:0)

试试这个

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

只需替换

TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(time_out)-TIME_TO_SEC(time_in))),'%H:%i') as total_wh

SEC_TO_TIME(SUM(TIME_TO_SEC(time_out)-TIME_TO_SEC(time_in))) as total_wh

它输出为

enter image description here 我会帮助你。

答案 2 :(得分:0)

您可以像这样更改代码

     <?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>";
                ?>