Php 如何计算从相同日期花费的小时数
我有一个数据库表Php 如何计算从相同日期花费的小时数,php,mysql,Php,Mysql,我有一个数据库表tbl\u employees\u attention,当我运行查询查找考勤时,该表有in\u time和out\u time列,其中包含employeer\u id $curr_date = ''; foreach($atts AS $row) { $in_time_date = date('Y-m-d', strtotime($row->in_time)); if($curr_date != $in_time_date){ $spend_
tbl\u employees\u attention
,当我运行查询查找考勤时,该表有in\u time
和out\u time
列,其中包含employeer\u id
$curr_date = '';
foreach($atts AS $row) {
$in_time_date = date('Y-m-d', strtotime($row->in_time));
if($curr_date != $in_time_date){
$spend_time = strtotime($row->out_time) - strtotime($row->in_time);
$curr_date = $in_time_date;
}else{
$spend_time += strtotime($row->out_time) - strtotime($row->in_time);
}
}
返回的结果如下所示:
+========================================+
| out_time | in_time |
+========================================+
2019-11-11 18:00:00 - 2019-11-11 09:00:00
2019-11-12 10:00:00 - 2019-11-12 09:00:00
2019-11-12 15:00:00 - 2019-11-12 13:00:00
2019-11-13 18:00:00 - 2019-11-13 09:00:00
2019-11-13 20:00:00 - 2019-11-13 18:00:00
2019-11-14 18:00:00 - 2019-11-14 09:00:00
+========================================+
2019-11-11 => 09:00:00
2019-11-12 => 03:00:00
2019-11-13 => 10:00:00
2019-11-14 => 9:00:00
在出席列表中,有多个相同的日期,现在我不会按组日期获取总花费时间,如下所示:
+========================================+
| out_time | in_time |
+========================================+
2019-11-11 18:00:00 - 2019-11-11 09:00:00
2019-11-12 10:00:00 - 2019-11-12 09:00:00
2019-11-12 15:00:00 - 2019-11-12 13:00:00
2019-11-13 18:00:00 - 2019-11-13 09:00:00
2019-11-13 20:00:00 - 2019-11-13 18:00:00
2019-11-14 18:00:00 - 2019-11-14 09:00:00
+========================================+
2019-11-11 => 09:00:00
2019-11-12 => 03:00:00
2019-11-13 => 10:00:00
2019-11-14 => 9:00:00
问题是循环何时运行日期分组不正确。请提供$atts的
var\u export
,以便我们可以轻松测试它?是的,这段代码中有什么问题,我在顶部显示foreach循环,然后显示循环返回结果,然后显示我想要的内容。$atts是select查询的变量。像$atts=select*from tbl_employees_attention where employees_id=103
这对我们的测试没有帮助,但是我们没有您的数据库可用。