Scroll to top

Calculate business days between two dates (working weekdays between two dates)

$startdate = “5-11-2019”;
$enddate = “17-11-2019”

$businessdays = number_of_working_days($startdate, $enddate);

echo $businessdays;

function number_of_working_days($from, $to) {
    $workingDays = [1, 2, 3, 4, 5]; # date format = N (1 = Monday, …)

    $from = new DateTime($from);
    $to = new DateTime($to);
    $to->modify(‘+1 day’);
    $interval = new DateInterval(‘P1D’);
    $periods = new DatePeriod($from, $interval, $to);

    $days = 0;
    foreach ($periods as $period) {
        if (!in_array($period->format(‘N’), $workingDays)) continue;
        $days++;
    }
    return $days;
}

Leave a Reply

Your email address will not be published. Required fields are marked *