38 lines
961 B
PHP
38 lines
961 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\WorkingDays\Date;
|
|
|
|
use App\Services\WorkingDays\WorkingDayDeterminerInterface;
|
|
|
|
/**
|
|
* Determines if a given date is a working day (Monday to Friday)
|
|
*/
|
|
class MonToFriWorkingDayDeterminer implements WorkingDayDeterminerInterface
|
|
{
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
public function isWorkingDay(\DateTimeImmutable $date): bool
|
|
{
|
|
$weekday = $date->format('N');
|
|
return $weekday <= 5; // Monday to Friday
|
|
}
|
|
|
|
public function getWorkingDaysCount(\DateTimeImmutable $startDate, int $workingDays): int
|
|
{
|
|
$period = new \DatePeriod($startDate, new \DateInterval('P1D'), $startDate->add(new \DateInterval(sprintf('P%dD', $workingDays))));
|
|
$workingDaysCount = 0;
|
|
|
|
foreach ($period as $date) {
|
|
if ($this->isWorkingDay($date)) {
|
|
$workingDaysCount++;
|
|
}
|
|
}
|
|
|
|
return $workingDaysCount;
|
|
}
|
|
}
|