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

How to destroy OpeningHours object or update it ?

vipera7 opened this issue · comments

Hello,

I have a scenario where I need to modify the exception for every entities. I want to update the exceptions for each entity separately as they have unique exceptions. How can I achieve this?

For example, I'm currently using a for loop for each entity in PHP:

// Create openingHours objects here
foreach ($entities as $entity) {
    // or create openingHours objects here or update exceptions
    $diffInSeconds = round($openingHours->diffInOpenSeconds(new DateTime($entity['start']), new DateTime($entity['end'])));
    // Destroy openingHours objects
}
commented

Hello,

Nothing particular, create the OpeningHours instance in your loop.

$regularWeek = [
    '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'     => [],
];

foreach ($entities as $entity) {
    $openingHours = OpeningHours::create([
        ...$regularWeek
        'exceptions' => $entity->getOpeningHoursExceptions(),
    ]);
    $diffInSeconds = round($openingHours->diffInOpenSeconds(new DateTime($row['entity']), new DateTime($row['entity'])));
}