Calculating working hours between two dates

后端 未结 3 1852
慢半拍i
慢半拍i 2020-12-09 13:23

I need a PHP method for calculating working hours between two dates based on a 8 hour working day and excluding weekends and bank holidays.

For example the differenc

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 14:26

    Function below calculates working hours between two dates, provided in text format such as '2013-11-27 13:40', taking work day hours from 9 to 17 (can be changed).

    function get_working_hours($from,$to)
    {
        // timestamps
        $from_timestamp = strtotime($from);
        $to_timestamp = strtotime($to);
    
        // work day seconds
        $workday_start_hour = 9;
        $workday_end_hour = 17;
        $workday_seconds = ($workday_end_hour - $workday_start_hour)*3600;
    
        // work days beetwen dates, minus 1 day
        $from_date = date('Y-m-d',$from_timestamp);
        $to_date = date('Y-m-d',$to_timestamp);
        $workdays_number = count(get_workdays($from_date,$to_date))-1;
        $workdays_number = $workdays_number<0 ? 0 : $workdays_number;
    
        // start and end time
        $start_time_in_seconds = date("H",$from_timestamp)*3600+date("i",$from_timestamp)*60;
        $end_time_in_seconds = date("H",$to_timestamp)*3600+date("i",$to_timestamp)*60;
    
        // final calculations
        $working_hours = ($workdays_number * $workday_seconds + $end_time_in_seconds - $start_time_in_seconds) / 86400 * 24;
    
        return $working_hours;
    }
    

    There are two additional functions. One returns work days array...

    function get_workdays($from,$to) 
    {
        // arrays
        $days_array = array();
        $skipdays = array("Saturday", "Sunday");
        $skipdates = get_holidays();
    
        // other variables
        $i = 0;
        $current = $from;
    
        if($current == $to) // same dates
        {
            $timestamp = strtotime($from);
            if (!in_array(date("l", $timestamp), $skipdays)&&!in_array(date("Y-m-d", $timestamp), $skipdates)) {
                $days_array[] = date("Y-m-d",$timestamp);
            }
        }
        elseif($current < $to) // different dates
        {
            while ($current < $to) {
                $timestamp = strtotime($from." +".$i." day");
                if (!in_array(date("l", $timestamp), $skipdays)&&!in_array(date("Y-m-d", $timestamp), $skipdates)) {
                    $days_array[] = date("Y-m-d",$timestamp);
                }
                $current = date("Y-m-d",$timestamp);
                $i++;
            }
        }
    
        return $days_array;
    }
    

    and second - returns holidays array

    function get_holidays() 
    {
        // arrays
        $days_array = array();
    
        // You have to put there your source of holidays and make them as array...
        // For example, database in Codeigniter:
        // $days_array = $this->my_model->get_holidays_array();
    
        return $days_array;
    }
    

提交回复
热议问题