Use OpeningHours::create() to initialize a schedule from an array. If no timezone is provided, the package assumes provided DateTime objects already match the schedule's timezone.
You can specify an input and output timezone to handle conversions automatically. If an output timezone is provided, methods returning dates (like nextOpen) will convert the result back to the original timezone so the object reflects local time while the internal logic uses the business timezone.
To handle overlapping ranges during creation, use the 'overflow' => true option in the array or use the createAndMergeOverlappingRanges() shortcut.
// Basic creation
$openingHours = OpeningHours::create([
'monday' => ['09:00-12:00', '13:00-18:00'],
]);
// Creation with specific input and output timezones
$openingHours = OpeningHours::create([
'monday' => ['09:00-12:00', '13:00-18:00'],
'timezone' => [
'input' => 'America/New_York',
'output' => 'Europe/Oslo',
],
]);
// Merging overlapping ranges
$ranges = [
'monday' => ['08:00-11:00', '10:00-12:00'],
];
$mergedRanges = OpeningHours::mergeOverlappingRanges($ranges); // Monday becomes ['08:00-12:00']
$openingHours = OpeningHours::createAndMergeOverlappingRanges($ranges);