我有一個(gè)$attendace變量包含來(lái)自Laravel查詢構(gòu)建器的集合: $attendance = collect(DB::table('attendance_copy')->where('emp_number', Auth::user()->emp_number)->where('date_created', $datenow)->get()); 這是結(jié)果: [ { "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" } ]; 我到目前為止所做的(當(dāng)只有1個(gè)start break和end break時(shí)有效): $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); 但在我的例子中,每個(gè)員工全天都會(huì)記錄很多休息時(shí)間。我想計(jì)算一下員工的休息時(shí)間: 從上面的集合(12hr格式)來(lái)看,它應(yīng)該是01:00 total hrs。從7:30 to 8:00 is 30mins和8:30 to 9:00 is another 30mins開始。將有5個(gè)最長(zhǎng)中斷時(shí)間。 這可能嗎?或者我應(yīng)該重新設(shè)計(jì)我的出席表? 此答案假定集合已由employee_number篩選,并且Start Break之后的狀態(tài)必須為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是秒數(shù)。gmdate函數(shù)將其轉(zhuǎn)換為小時(shí)、分鐘、秒,返回01:00:00。 |
免責(zé)聲明:本站部分文章和圖片均來(lái)自用戶投稿和網(wǎng)絡(luò)收集,旨在傳播知識(shí),文章和圖片版權(quán)歸原作者及原出處所有,僅供學(xué)習(xí)與參考,請(qǐng)勿用于商業(yè)用途,如果損害了您的權(quán)利,請(qǐng)聯(lián)系我們及時(shí)修正或刪除。謝謝!
始終以前瞻性的眼光聚焦站長(zhǎng)、創(chuàng)業(yè)、互聯(lián)網(wǎng)等領(lǐng)域,為您提供最新最全的互聯(lián)網(wǎng)資訊,幫助站長(zhǎng)轉(zhuǎn)型升級(jí),為互聯(lián)網(wǎng)創(chuàng)業(yè)者提供更加優(yōu)質(zhì)的創(chuàng)業(yè)信息和品牌營(yíng)銷服務(wù),與站長(zhǎng)一起進(jìn)步!讓互聯(lián)網(wǎng)創(chuàng)業(yè)者不再孤獨(dú)!
掃一掃,關(guān)注站長(zhǎng)網(wǎng)微信