Business Hours

Available versions:

Installation

composer require aeon-php/business-hours

Basics

BusinessHours holds whole logic of this library. It checks 3 collections in given order to determine if during given date and time business is open or not. Those 3 collections are described below:

  • BusinessDays $regularBusinessDays - lowest priority when checking open hours, overwrites nothing
  • BusinessDays $customBusinessDays - highest priority when checking open hours, overwrites business days and non business days
  • NonBusinessDays $nonBusinessDays - medium priority when checking open hours, overwrites regular business days

Lets see some examples.

Linear Working Hours

This library provides simple abstraction over Business Hours. The easiest example where there are no holidays and business working Mon-Fri from 6am and 6pm is presented below.

<?php

use Aeon\Calendar\Gregorian\BusinessHours;
use Aeon\Calendar\Gregorian\BusinessHours\WorkingHours\LinearWorkingHours;
use Aeon\Calendar\Gregorian\Time;
use Aeon\Calendar\Gregorian\DateTime;

$businessHours = new BusinessHours(
    $regularBusinessDays = BusinessHours\BusinessDays::mondayFriday(
        new LinearWorkingHours(Time::fromString('6am'), Time::fromString('6pm'))
    ),
    BusinessHours\BusinessDays::none(),
    BusinessHours\NonBusinessDays::none()
);

$businessHours->isOpen(DateTime::fromString('2020-01-03 8am')); // true
// 2020-01-11 - Saturday
$businessHours->nextBusinessDay(Day::fromString('2020-01-11')); // 2020-01-13
Working Hours with Holidays

Thanks to integration with Aeon Holidays Library previous example can be decorated with regional holidays.

<?php

use Aeon\Calendar\Gregorian\BusinessHours;
use Aeon\Calendar\Gregorian\BusinessHours\WorkingHours\LinearWorkingHours;
use Aeon\Calendar\Gregorian\Time;
use Aeon\Calendar\Gregorian\Day;
use Aeon\Calendar\Gregorian\Holidays\GoogleCalendar\CountryCodes;
use Aeon\Calendar\Gregorian\Holidays\GoogleCalendarRegionalHolidays;

$businessHours = new BusinessHours(
    $regularBusinessDays = BusinessHours\BusinessDays::wholeWeek(
        $weekWorkingHours = new LinearWorkingHours(Time::fromString('6am'), Time::fromString('6pm')),
        $weekendWorkingHours = new LinearWorkingHours(Time::fromString('11am'), Time::fromString('6pm'))
    ),
    $customBusinessDays = new BusinessHours\BusinessDays(
        new BusinessHours\BusinssDay\CustomBusinessDay(
            Day::fromString('2020-01-01'),
            new LinearWorkingHours(Time::fromString('11am'), Time::fromString('3pm'))
        )
    ),
    $nonBusinessDay = new BusinessHours\NonBusinessDays(
        new BusinessHours\NonBusinessDay\Holidays(
            new GoogleCalendarRegionalHolidays(CountryCodes::PL)
        )
    )
);

$businessHours->isOpenOn(Day::fromString('2020-06-11')); // false