Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

username1's avatar

How to get total working hours excluding lunch break and holidays.

good day, I want to exclude lunch break in total working hours, holidays seems to work only lunch break wont, I don't know what could be the condition to apply, I just new here please help me. here is my code in blade..

,,, <?php $start_time = Carbon\Carbon::createFromFormat('H:i:s', "08:15:00"); $end_time = Carbon\Carbon::createFromFormat('H:i:s', "16:45:00");

                                        $work_date_start = Carbon\Carbon::createFromFormat('Y-m-d', date('Y-m-d', strtotime($works->start))); //date start
                                        $work_start_time = Carbon\Carbon::createFromFormat('H:i:s', date('H:i:s', strtotime($works->start))); //time start
                                        $work_date_end = Carbon\Carbon::createFromFormat('Y-m-d', date('Y-m-d', strtotime($works->end))); //date start
                                        $work_end_time = Carbon\Carbon::createFromFormat('H:i:s', date('H:i:s', strtotime($works->end))); //time start
                                        $time = 0;
                                        $total_hours = 0;

                                            //Calculate time difference from start to end (same date) 
                                        if($work_date_start == $work_date_end){
                                            $time = $works->start->diffInMinutes($works->end); 
                                            
                                            $total_hours += $time;

                                            Log::info('TOTAL HOURS1:' .$total_hours. '| START:' .$works->start. '| END:' .$works->end. '| JO:' .$data->getJobOrder->jo_no);
                                            
                                        }else{
                                            // Calculate the time difference from work start (current date)
                                            if(strtotime($work_start_time) >= strtotime($start_time) && strtotime($work_start_time) <= strtotime($end_time)){
                                                $time += $work_start_time->diffInMinutes($end_time);
                                                Log::info('TOTAL HOURS_T1:' .$time. '| START:' .$works->start. '| END:' .$works->end. '| JO:' .$data->getJobOrder->jo_no);
                                            }
                                    
                                            // Calculate the time difference from work end (next date) 
                                            if(strtotime($work_end_time) >= strtotime($start_time) && strtotime($work_end_time) <= strtotime($end_time)){
                                                $time += $start_time->diffInMinutes($work_end_time);
                                                Log::info('TOTAL HOURS_T2:' .$time. '| START:' .$works->start. '| END:' .$works->end. '| JO:' .$data->getJobOrder->jo_no);
                                            }
                                    
                                            //If multiple days are different  (multiple date)
                                            $current_date = Carbon\Carbon::createFromFormat('Y-m-d', date('Y-m-d', strtotime($works->start)));
                                            if($work_date_start->diffInDays($work_date_end) > 1){
                                                for ($i=0; $i < $work_date_start->diffInDays($work_date_end); $i++) { 
                                                    // Get the current date
                                                    $current_date->addDay();
                                                    if($holidays != null){
                                                        foreach ($holidays as $data) {
                                                            if(date('Y-m-d', strtotime($current_date)) == date('Y-m-d', strtotime($data->date))){
                                                                continue;
                                                            }
                                                        }
                                                    }elseif(date('l', strtotime($current_date)) == "Sunday"){
                                                        continue;
                                                    }elseif (date('Y-m-d', strtotime($current_date)) != date('Y-m-d', strtotime($work_date_end))){
                                                        $date_hours_diff = 8;
                                                        $time += 60 * $date_hours_diff; 

                                                        Log::info('TOTAL HOURS_T3:' .$time. '| START:' .$works->start. '| END:' .$works->end. '| JO:' .$data->getJobOrder->jo_no);
                                                    }

                                                }
                                            }
                                          
                                            $total_hours += $time;
                                            // Log::info('TOTAL HOURS2:' .$total_hours. '| START:' .$works->start. '| END:' .$works->end. '| JO:' .$data->getJobOrder->jo_no);
                                        
                                        }
                                        //ACTUAL TIME
                                        $hours = floor($total_hours / 60);
                                        $minutes =  $total_hours % 60;
                                        $actual_time = $hours + round($minutes/ 60, 2);

                                        //EFFICIENCY
                                        if($actual_time != 0) {
                                        $efficiency = ($works->allot_hours / $actual_time) * 100;
                                        }else{
                                        $efficiency = 0; 
                                        }
                                        
                                        //TOTAL ACTUAL TIME
                                        $total_all_actual_time += $actual_time;
                                        //TOTAL COMMITED TIME
                                        $total_all_commited_time += $works->allot_hours;                                          
                                    ?>
0 likes
3 replies
sr57's avatar

@username1

only lunch break wont, I don't know what could be the condition to apply

Assuming "lunch break" is a fix number of minutes per working day ( $lbd ) , you can :

  • calculate the number of working days in your loop excluding holidays -> $nwd

  • at the end, substract $lbd * $nbd from your result

Snapey's avatar

A suggestion.

move this code into some function (anywhere but in the view)

learn how to use Carbon and its diff function

If the person takes 30 minutes for lunch, just subtract 30 from the total

username1's avatar

@Snapey thank you for the suggestion, but can you tell me where to put this function? should i put in model or a controller? pls give me some advice I'm just new in laravel thank you.

Please or to participate in this conversation.