Git Product home page Git Product logo

business-hours's Introduction

Business Hours

Latest Version on Packagist Software License Build Status 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.

business-hours's People

Contributors

dragosprotung avatar cezarystepkowski avatar hanzgeeratz avatar

Stargazers

MarcS avatar Christoffer Lindahl Hassel avatar Ionut Marinescu avatar Toby Berster avatar Martin Lechêne avatar Andreza Pollyana avatar

Watchers

James Cloos avatar  avatar  avatar Adam avatar  avatar

business-hours's Issues

Support for PHP 8 in a public release

Hi guys, thanks for the great package.

Is it possible you could make a release with PHP 8 support on your 2.* branch? (or even 3.0, but I'm not sure about the plans for releasing this). Just browsing through the code I don't see any reason why the latest release of v2 wouldn't be compatible with PHP 8, but I could be wrong.

Thanks again.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.