fastbolt/working-day-provider

PHP package to ease calculation of number of working days between two dates.

v1.0.0 2023-04-14 06:35 UTC

This package is auto-updated.

Last update: 2024-04-14 08:55:41 UTC


README

Composer version

Code Climate maintainability Test Coverage

Type Coverage Psalm Level

Github Build

Working day provider

PHP package to ease calculation of number of working days between two given dates.

Prerequisites

For now, the bundle is tested using PHP 7.4, 8.0 and 8.1.

Installation

The library can be installed via composer:

composer require fastbolt/working-day-provider

Usage

Basic usage

For the most basic usage, you can use the WorkingDayProvider without any configuration.

By default, it will not use any application or region specific holiday, but only consider monday to friday as working days.

use Fastbolt\WorkingDayProvider\WorkingDayProvider;

$workingDayProvider = new WorkingDayProvider();
$workingDays = $workingDayProvider->getWorkingDaysForPeriod(
    new DateTimeImmutable('2023-01-01'),
    new DateTimeImmutable('2023-01-07')
);

// $workingDays will be 5

Application / region specific holidays

The library is designed to include application / region specific holidays as "non-working days".

To do so, you need to create a class implementing the Fastbolt\WorkingDayProvider\Holiday\HolidayProvider interface.

The only interface method getHolidaysForDateRange must return an array of objects implementing the \Fastbolt\WorkingDayProvider\Holiday\Holiday interface.

use Acme\Provider\HolidayProvider;
use Fastbolt\WorkingDayProvider\WorkingDayProvider;

$holidayProvider = new HolidayProvider());

# Example `$holidayProvider->getHolidaysForDateRange(2022-12-24, 2022-12-26)` returns one holiday for 26th of december.
$workingDayProvider = new WorkingDayProvider($holidayProvider);
$workingDays = $workingDayProvider->getWorkingDaysForPeriod(
    new DateTimeImmutable('2022-12-24'),
    new DateTimeImmutable('2022-12-26')
);

// $workingDays will be 0 (days are saturday, sunday and holiday)

Custom configuration (not yet fully-implemented)

Optionally, you can provide a custom configuration to the WorkingDayProvider constructor.

use Fastbolt\WorkingDayProvider\Configuration;
use Fastbolt\WorkingDayProvider\WorkingDayProvider;

$configuration = new Configuration(['excludeWeekDays' => [1, 6, 7]);
$workingDayProvider = new WorkingDayProvider(null, $configuration);
$workingDays = $workingDayProvider->getWorkingDaysForPeriod(
    new DateTimeImmutable('2022-12-24'),
    new DateTimeImmutable('2022-12-26')
);

// $workingDays will be 0 (working days monday, saturday and sunday are all configured as non-working days)