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

Exceptions with date ranges

andimg93 opened this issue · comments

Hey,

as far as I see the related code it is not possible, right now, that I can define a date range as exception.

Like

'exceptions' => [
     '2016-11-11-2016-11-25' => ['09:00-12:00'],
]

or am I wrong?

That would be a possible feature request.

commented

Hello, you can build your $exceptions list with PHP native DatePeriod:

$exceptions = [];

foreach (new DatePeriod(new DateTimeImmutable('2016-11-11'), new DateInterval('P1D'), new DateTimeImmutable('2016-11-25')) as $day) {
    $exceptions[$day->format('Y-m-d')] = ['09:00-12:00'];
}

//...
'exceptions' => $exceptions,

Hey @kylekatarnls of course I can and this is what I would do as workaround but the point is I want to save such opening hours in database and it is unnecessary extra data for the related ranges therefore I asked.

So saving e.g.: '2016-11-11-2016-11-25' => ['09:00-12:00'], would be much less as:
'2016-11-11' =>
['09:00-12:00'],
'2016-11-12' =>
['09:00-12:00'],
'2016-11-13' =>
['09:00-12:00'],
'2016-11-14' =>
['09:00-12:00'],
'2016-11-15' =>
['09:00-12:00'],
'2016-11-16' =>
['09:00-12:00'],
'2016-11-17' =>
['09:00-12:00'],
'2016-11-18' =>
['09:00-12:00'],
'2016-11-19' =>
['09:00-12:00'],
'2016-11-20' =>
['09:00-12:00'],
'2016-11-21' =>
['09:00-12:00'],
'2016-11-22' =>
['09:00-12:00'],
'2016-11-23' =>
['09:00-12:00'],
'2016-11-24' =>
['09:00-12:00'],

also this is a simple example, imagine several opening hours with a ton of exceptions - therefore you get a ton of unnecessary data to save in db.

commented

Hello, I suggest you just add a customization step:

I also suggest to use some explicit separator, here in the example I used ' to ' so it's more readable and does not overlap - which is already used by date format.

$openingHoursDescription = [
    'monday to friday'  => ['09:00-12:00', '13:00-18:00'],
    'saturday to sunday'=> [],
    'exceptions' => [
        '2016-11-11 to 2016-11-25' => ['09:00-12:00'],
    ],
];

function parseOpeningHoursDescription(array $description): array
{
    foreach ($description as $key => $value) {
        if (is_array($value)) {
            $value = parseOpeningHoursDescription($value);
        }

        $parts = explode(' to ', $key);

        if (count($parts) === 2) {
            unset($description[$key]);
            $isDayName = preg_match('/^[a-z]+$/i', $parts[0]);
            $start = new DateTimeImmutable($parts[0]);
            $end = $start->modify($parts[1])->modify('+12 hours');
            // If using PHP 8.2, you can use DatePeriod::INCLUDE_END_DATE instead of ->modify('+12 hours')

            foreach (new DatePeriod($start, new DateInterval('P1D'), $end) as $day) {
                $description[$day->format($isDayName ? 'l' : 'Y-m-d')] = $value;
            }

            continue;
        }

        $description[$key] = $value;
    }

    return $description;
}

$openingHours = OpeningHours::create(parseOpeningHoursDescription($openingHoursDescription));

Then you can save $openingHoursDescription (before the transformation) in your database.

We might think about supporting this syntax out of the box, but need to think about the consequences of supporting multiple syntax for the same result, what if a day appears in multiple ranges and other edge cases.

Hello @kylekatarnls

Monday to Friday and stuff like that so for the common weekdays is not something what I would recommend to support for the related range feature request from my side. The reason for it is that there the related overflow is not relevant.

I just talk about the exceptions as there you often go with holidays and stuff like that which take longer than the usual opening hours.

So I end up more like something like the following as fix on my side, but anyway I see it more as a feature request where exactly this is out of the box possible, for the exceptions - so simple said exceptions supporting (date) ranges:

$openingHoursDescription = [
    'monday'  => ['09:00-12:00', '13:00-18:00'],
    'tuesday' => ['09:00-12:00', '13:00-18:00'],
    'wednesday' => ['09:00-12:00', '13:00-18:00'],
    'thursday' => ['09:00-12:00', '13:00-18:00'],
    'friday' => ['09:00-12:00', '13:00-18:00'],
    'saturday' => [],
    'sunday' => [],
    'exceptions' => [
        '2016-11-11 to 2016-11-25' => ['09:00-12:00'],
        // Add other exception ranges as needed
    ],
];

function parseExceptionRanges(array $description): array
{
    $exceptions = $description['exceptions'] ?? [];
    unset($description['exceptions']);

    foreach ($exceptions as $key => $value) {
        $parts = explode(' to ', $key);

        if (count($parts) === 2) {
            $start = new DateTimeImmutable($parts[0]);
            $end = new DateTimeImmutable($parts[1])->modify('+1 day'); // End date is exclusive

            foreach (new DatePeriod($start, new DateInterval('P1D'), $end) as $day) {
                $description[$day->format('Y-m-d')] = $value;
            }
        }
    }

    return $description;
}

$openingHours = OpeningHours::create(parseExceptionRanges($openingHoursDescription));
commented

The reason for it is that there the related overflow is not relevant.

Responsibility of spatie/opening-hours is not to ensure the passed payload can be stored below a certain size. The size of you data will depend on your case, and where you store it and how big it can be will also vary from one app to another. How to compress it is application responsibility, the library should remain agnostic.

If we implement a new feature/syntax, it has to be as generic and consistent as possible, not to solve this one specific issue but to cover various needs where ranges support can be convenient.