spatie / opening-hours

Query and format a set of opening hours

Home Page:https://freek.dev/595-managing-opening-hours-with-php

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for holidays

ziming opened this issue · comments

commented

Currently when I try to pass the holiday options of https://github.com/kylekatarnls/business-time , it result in error, will be nice if this package could allow the holidaysAreClosed and holidays config of the busienss-time package

Another question would be how is this different from the Schedule class in https://github.com/kylekatarnls/business-time ?

// Example from that package
use Carbon\CarbonImmutable;
use BusinessTime\Schedule;

$us = Schedule::create([
    'monday'            => ['09:00-12:00', '13:00-18:00'],
    'tuesday'           => ['09:00-12:00', '13:00-18:00'],
    'wednesday'         => ['09:00-12:00'],
    'thursday'          => ['09:00-12:00', '13:00-18:00'],
    'friday'            => ['09:00-12:00', '13:00-20:00'],
    'holidaysAreClosed' => true,
    'holidays'          => [
        'region' => 'us-ny',
        'with'   => [
            'labor-day'               => null,
            'company-special-holiday' => '04-07',
        ],
    ],
]);

$fr = Schedule::create([
    'monday'            => ['08:00-12:00', '13:00-17:00'],
    'tuesday'           => ['08:00-12:00', '13:00-17:00'],
    'wednesday'         => ['08:00-12:00'],
    'thursday'          => ['08:00-12:00', '13:00-17:00'],
    'friday'            => ['08:00-12:00', '13:00-17:00'],
    'holidaysAreClosed' => true,
    'holidays'          => [
        'region' => 'fr-national',
        'with'   => [
            'company-special-holiday' => '24/8',
        ],
    ],
]);

$d = CarbonImmutable::parse('2022-10-21 06:40:00');
echo $us->subOpenHours($d, 1)->format('Y-m-d H:i:s'); // 2022-10-20 17:00:00
echo $fr->subOpenHours($d, 1)->format('Y-m-d H:i:s'); // 2022-10-20 16:00:00

$d = CarbonImmutable::parse('2022-10-20 17:30:00');
var_dump($us->isOpen($d)); // true
var_dump($fr->isOpen($d)); // false

Thanks!

commented

Hello,

it result in error

Please print the error, the actual output and the output you expect. The example you copy-paste from the doc works in the sandbox https://play.phpsandbox.io/embed/nesbot/carbon, also in unit tests https://github.com/spatie/opening-hours/actions and in the various contexts I tested it, if you a different output, please show this output and give details about your other dependencies, versions and the rest of the code that might be involved.

Another question would be how is this different from the Schedule class in https://github.com/kylekatarnls/business-time ?

What "this" refers to in this sentence? Schedule is part of the business-time package, it's a way to use it as a service (isolated) without enabling global macros on the Carbon class.

commented

Hi,

The code example earlier I give is for my 2nd question, not for my 1st question. For my 1st question. The code example that results in an error is:

use Spatie\OpeningHours\OpeningHours;

$openingHours = OpeningHours::create([
    'monday'     => ['09:00-12:00', '13:00-18:00'],
    'tuesday'    => ['09:00-12:00', '13:00-18:00'],
    'wednesday'  => ['09:00-12:00'],
    'thursday'   => ['09:00-12:00', '13:00-18:00'],
    'friday'     => ['09:00-12:00', '13:00-20:00'],
    'saturday'   => ['09:00-12:00', '13:00-16:00'],
    'sunday'     => [],
    'holidaysAreClosed' => true,
  // Note that exceptions will still have the precedence over
  // the holidaysAreClosed option.
  'holidays' => [
    'region' => 'us-ny', // Load the official list of holidays from USA - New York
    'with' => [
      'labor-day' => null, // Remove the Labor Day (so the business is open)
      'company-special-holiday' => '04-07', // Add some custom holiday of your company 
    ],
]]);

The error is Spatie\OpeningHours\Exceptions\InvalidDayName with message 'Day holidaysareclosedisn't a valid day name. Valid day names are lowercase english words, e.g.monday, thursday.'

If I remove holidaysareclosed I get Spatie\OpeningHours\Exceptions\InvalidDayName with message 'Day holidaysisn't a valid day name. Valid day names are lowercase english words, e.g.monday, thursday.'

For my 2nd question, I'm asking about what is the difference between the OpeningHours class in spatie/opening-hours and the Schedule class in your kylekatarnls/business-time as they both seem very similar (https://github.com/kylekatarnls/business-time#multiple-business-hours)

I also now have a new question (3rd question), which is where in kylekatarnls/business-time can I make PRs to to modify public holidays for a particular country? or does kylekatarnls/business-time fetch the countries holidays from some other online source? I tried to look at https://github.com/kylekatarnls/business-time/blob/master/src/BusinessTime/Traits/Holidays.php but I cannot find it there

Lastly, it looks like ur a jedi outcast fan, that is a great game

Thank you

commented

Ah OK, I see, so indeed, holidays can't be used in spatie/opening-hours.

business-time uses business-day to handle holidays + spatie/opening-hours to handle the hours in the week. This allow those who need just one feature to install a smaller package.

I don't think we should implement here the holidays, mostly if we do the same as in Schedule, as it would make it redundant.

What would prevent you to use business-time?

commented

Ah so I use uninstall spatie/opening-hours and just use your business-time Schedule package instead as Schedule is more feature packed than OpeningHours?

Okay then I shall use the Schedule class instead, hopefully it has all the methods that OpeningHours has.

As for my last question, which files in your business-times package should i edit and make PR if i want to modify a country official holidays? I can't seem to find the right file in your repo to do so.

commented

Schedule has all the features and in any case, you can $us->getOpeningHours() and you'll get the OpeningHours instance used.

Holidays lists are here:
https://github.com/kylekatarnls/business-day/tree/master/src/Cmixin/Holidays