我有一个$attendace变量包含来自Laravel查询构建器的集合: $attendance = collect(DB::table('attendance_copy')->where('emp_number', Auth::user()->emp_number)->where('date_created', $datenow)->get()); 这是结果: [ { "row_id":65, "emp_number":"IPPH0004", "time_stamp":"01:00:00", "attendance_status":"Punch In", "date_created":"2021-10-02" }, { "row_id":68, "emp_number":"IPPH0004", "time_stamp":"07:30:00", "attendance_status":"Start Break", "date_created":"2021-10-02" }, { "row_id":69, "emp_number":"IPPH0004", "time_stamp":"08:00:00", "attendance_status":"End Break", "date_created":"2021-10-02" }, { "row_id":70, "emp_number":"IPPH0004", "time_stamp":"08:30:00", "attendance_status":"Start Break", "date_created":"2021-10-02" }, { "row_id":71, "emp_number":"IPPH0004", "time_stamp":"09:00:00", "attendance_status":"End Break", "date_created":"2021-10-02" } ]; 我到目前为止所做的(当只有1个start break和end break时有效): $startbreak = strtotime(( isset( $attendance->where('attendance_status', 'Start Break')->first()->time_stamp ) == null ? "00:00:00" : $attendance->where('attendance_status', 'Start Break')->first()->time_stamp));$endbreak = strtotime(( isset( $attendance->where('attendance_status', 'End Break')->first()->time_stamp ) == null ? "00:00:00" : $attendance->where('attendance_status', 'End Break')->first()->time_stamp));$minsbreak = date('i',$endbreak - $startbreak); 但在我的例子中,每个员工全天都会记录很多休息时间。我想计算一下员工的休息时间: 从上面的集合(12hr格式)来看,它应该是01:00 total hrs。从7:30 to 8:00 is 30mins和8:30 to 9:00 is another 30mins开始。将有5个最长中断时间。 这可能吗?或者我应该重新设计我的出席表? 此答案假定集合已由employee_number筛选,并且Start Break之后的状态必须为End Break。 $total_break_time = 0; for ($i = 0; $i < count($attendance); ++$i) { if ($i == 0) continue; if ($attendance[$i-1]['attendance_status'] == 'Start Break') { $previous_timestamp = strtotime($attendance[$i-1]['date_created'] . ' ' . $attendance[$i-1]['time_stamp']); $current_timestamp = strtotime($attendance[$i]['date_created'] . ' ' . $attendance[$i]['time_stamp']); $total_break_time += ($current_timestamp - $previous_timestamp); } } echo gmdate('H:i:s', $total_break_time) . PHP_EOL; $total_break_time是秒数。gmdate函数将其转换为小时、分钟、秒,返回01:00:00。 |
免责声明:本站部分文章和图片均来自用户投稿和网络收集,旨在传播知识,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系我们及时修正或删除。谢谢!
始终以前瞻性的眼光聚焦站长、创业、互联网等领域,为您提供最新最全的互联网资讯,帮助站长转型升级,为互联网创业者提供更加优质的创业信息和品牌营销服务,与站长一起进步!让互联网创业者不再孤独!
扫一扫,关注站长网微信