1

I am working on laravel project using carbon on date format. I getting stuck to find only difference of working hours. Let suppose my working hourse is (10am to 7pm) and we received order on 6pm and process on next day on 1pm so difference of hourse will be (4 hours). Similarly if we've weekend during order process and order received then weekend days also should be exclude. Please give me the solution.

2 Answers2

1

I guess do something like:

  • Calculate the remaining working hours for the day the order was placed on
  • Calculate the amount of full work days in between the placement and the processing and multiply by the amount of working hours in a day
  • Calculate the working hours for the day the order was processed on
  • Calculate the sum of the above
PtrTon
  • 3,705
  • 2
  • 14
  • 24
0

Thank you for the help, but I got the answer.

function getWorkingHours($ini_str,$end_str){
            //config
            $ini_time = [8,0]; //start working (hr, min)
            $end_time = [17,0]; //end working (hr, min)
            //date objects
            $ini = date_create($ini_str);
            $ini_wk = date_time_set(date_create($ini_str),$ini_time[0],$ini_time[1]);
            $end = date_create($end_str);
            $end_wk = date_time_set(date_create($end_str),$end_time[0],$end_time[1]);
            //days
            $workdays_arr = getWorkDays($ini,$end);
            $workdays_count = count($workdays_arr);
            $workday_seconds = (($end_time[0] * 60 + $end_time[1]) - ($ini_time[0] * 60 + $ini_time[1])) * 60;
            //get time difference
            $ini_seconds = 0;
            $end_seconds = 0;
            if(in_array($ini->format('Y-m-d'),$workdays_arr)) $ini_seconds = $ini->format('U') - $ini_wk->format('U');
            if(in_array($end->format('Y-m-d'),$workdays_arr)) $end_seconds = $end_wk->format('U') - $end->format('U');
            $seconds_dif = $ini_seconds > 0 ? $ini_seconds : 0;
            if($end_seconds > 0) $seconds_dif += $end_seconds;
            //final calculations
            $working_seconds = ($workdays_count * $workday_seconds) - $seconds_dif;
            $working_seconds_format =  gmdate("H:i:s", $working_seconds);
            return $working_seconds_format; //return hrs
        }
function getWorkDays($ini,$end){
            //config
            $skipdays = [5,6]; //friday:5; saturday:6; sunday:0
            $skipdates = []; //eg: ['2016-10-10'];
            //vars
            $current = clone $ini;
            $current_disp = $current->format('Y-m-d');
            $end_disp = $end->format('Y-m-d');
            $days_arr = [];
            //days range
            while($current_disp <= $end_disp){
                if(!in_array($current->format('w'),$skipdays) && !in_array($current_disp,$skipdates)){
                    $days_arr[] = $current_disp;
                }
                $current->add(new DateInterval('P1D')); //adds one day
                $current_disp = $current->format('Y-m-d');
            }
            return $days_arr;
        }
echo getWorkingHours('2019-10-10 20:00:00', '2019-10-13 19:59:30'); //thir-sun:

Ref: Calculating working hours between two dates