speicher210/business-hours

DateTime and business hours (opening, closing times) calculations

2.0.1 2017-12-13 11:43 UTC

This package is auto-updated.

Last update: 2020-08-04 20:05:52 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Install

Via Composer

$ composer require speicher210/business-hours

Change log

Please see CHANGELOG for more information what has changed recently.

Usage

<?php

declare(strict_types = 1);

use Speicher210\BusinessHours\BusinessHours;
use Speicher210\BusinessHours\BusinessHoursBuilder;
use Speicher210\BusinessHours\Day\AllDay;
use Speicher210\BusinessHours\Day\Day;
use Speicher210\BusinessHours\Day\DayBuilder;
use Speicher210\BusinessHours\Day\DayInterface;
use Speicher210\BusinessHours\Day\Time\Time;
use Speicher210\BusinessHours\Day\Time\TimeInterval;

require_once __DIR__ . '/vendor/autoload.php';

// Define the business hours.
$businessHours = new BusinessHours(
    [
        new Day(
            DayInterface::WEEK_DAY_MONDAY, // Day of the week.
            // Opening hours.
            [
                // Create a time interval
                new TimeInterval(
                    new Time(8, 0, 0),
                    new Time(18, 0, 0)
                )
            ]
        ),
        // Tuesday is opened all day (from 00:00 to 24:00).
        new AllDay(DayInterface::WEEK_DAY_TUESDAY),
        // For Wednesday we use Time::fromString
        new Day(
            DayInterface::WEEK_DAY_WEDNESDAY,
            [
                // First part of the day.
                new TimeInterval(
                    Time::fromString('10:00'),
                    Time::fromString('14:00')
                ),
                // Second part of the day.
                new TimeInterval(
                    Time::fromString('15:00'),
                    Time::fromString('20:00')
                ),
            ]
        ),
        // Thursday
        DayBuilder::fromArray(
            DayInterface::WEEK_DAY_THURSDAY,
            [
                // Overlapping time intervals will be merged.
                ['08:00', '11:00'],
                ['10:45', '12:15'],
                ['15:45', '22:00'],
                ['20:00', '24:00']
            ]
        )
    ],
    // The timezone for the opening hours.
    new \DateTimeZone('UTC')
);

// Check if you are within the opening hours.
$date = new \DateTime('2016-04-27 14:20', new \DateTimeZone('UTC'));
$businessHours->within($date); // false

// Various example to get the next or previous change (opening or closing).

$date = new \DateTime('2016-04-26 14:20', new \DateTimeZone('UTC'));
$businessHours->getPreviousChangeDateTime($date); // 2016-04-26 00:00:00
$businessHours->getNextChangeDateTime($date); // 2016-04-27 00:00:00

$date = new \DateTime('2016-04-28 10:55', new \DateTimeZone('UTC'));
$businessHours->getPreviousChangeDateTime($date); // 2016-04-28 08:00:00
$businessHours->getNextChangeDateTime($date); // 2016-04-28 12:15:00

$date = new \DateTime('2016-04-27 11:20', new \DateTimeZone('UTC'));
$businessHours->getPreviousChangeDateTime($date); // 2016-04-27 10:00:00
$businessHours->getNextChangeDateTime($date); // 2016-04-27 14:00:00

$date = new \DateTime('2016-04-28 01:00', new \DateTimeZone('UTC'));
$businessHours->getPreviousChangeDateTime($date); // 2016-04-27 20:00:00
$businessHours->getNextChangeDateTime($date); // 2016-04-28 08:00:00


$dateUTC = new \DateTime('2016-04-28 08:01', new \DateTimeZone('UTC'));
$var = $businessHours->within($dateUTC); // true

$businessHoursBerlin = BusinessHoursBuilder::shiftToTimezone($businessHours, new \DateTimeZone('Europe/Berlin'));
$dateBerlin = new \DateTime('2016-04-28 08:00', new \DateTimeZone('Europe/Berlin'));
$businessHoursBerlin->within($dateBerlin); // false

Testing

$ vendor/bin/phpunit

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email instead of using the issue tracker.

Credits

Original idea from https://github.com/florianv/business

License

The MIT License (MIT). Please see License File for more information.