简体   繁体   English

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

[英]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 它给出的输出为

在此处输入图片说明 I thing it will help you. 我认为这会对您有所帮助。

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.

相关问题 如何在特定时间段(24小时)内阻止IP地址 - How to block IP address for certain period of time (24 hours) 使用 PHP 从具有 start_time 和 end_time 的多维数组中查找总小时数? - Find total hours from multi-dimensional array with start_time and end_time using PHP? 如何在PHP中找到一定时间后的日期 - How to find date after certain period in php 如何从 PHP 数组中排除任意时间段(1-2 小时) - how to exclude an arbitrary period of time (1-2 hours) from the PHP array 如何使用php使用session.gc_maxlifetime适当地保持用户登录一定时间 - How to properly keep users logged in for a certain period of time with session.gc_maxlifetime using php PHP应用程序,用于使用oDesk API获取一段时间内的oDesk工作时间报告 - PHP application for getting oDesk reports of worked hours in a period of time using oDesk API PHP时间范围,获取总时间,晚上,晚上,周末和节假日 - PHP Time frame, get total hours, evening hours, night hours, weekend hours and holidays 如何使用PHP / Javascript在24小时内检查“错误”存在多少次 - How to check how many times an 'error' existed in a period of 24 hours using PHP/Javascript 如何在php中添加多个小时以拥有总小时数? - How do I add multiple hours in php to have the total hours? PHP发现时间没有花费在日期之间的特定时间段内 - PHP finding time not spent in certain time period between dates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM