我有一個$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。 |
免責聲明:本站部分文章和圖片均來自用戶投稿和網絡收集,旨在傳播知識,文章和圖片版權歸原作者及原出處所有,僅供學習與參考,請勿用于商業用途,如果損害了您的權利,請聯系我們及時修正或刪除。謝謝!

始終以前瞻性的眼光聚焦站長、創業、互聯網等領域,為您提供最新最全的互聯網資訊,幫助站長轉型升級,為互聯網創業者提供更加優質的創業信息和品牌營銷服務,與站長一起進步!讓互聯網創業者不再孤獨!
掃一掃,關注站長網微信
大家都在看