PHP

Business Days Between Two Dates

admin by @admin ADMIN
13m ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Count weekdays (Mon-Fri) between two dates, excluding an optional list of holidays. Direction-aware (works even if $end < $start).
PHP
Raw
<?php
function businessDays(DateTimeInterface $start, DateTimeInterface $end, array $holidays = []): int {
    if ($start > $end) [$start, $end] = [$end, $start];
    $hSet  = array_fill_keys(array_map(fn($d) => $d->format('Y-m-d'), $holidays), true);
    $count = 0;
    for ($d = clone $start; $d <= $end; $d = $d->modify('+1 day')) {
        $dow = (int)$d->format('N');               // 1=Mon … 7=Sun
        if ($dow >= 6) continue;
        if (isset($hSet[$d->format('Y-m-d')])) continue;
        $count++;
    }
    return $count;
}

$start    = new DateTime('2025-01-01');
$end      = new DateTime('2025-01-31');
$holidays = [new DateTime('2025-01-20')];     // MLK Day
echo businessDays($start, $end, $holidays);   // 22
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.