CPeriod
Introduction
Contoh Kode:
$period = new CPeriod('2021-01-01', '2021-02-01');
$oneMonth = CPeriod::month(1);
$thisMonth = CPeriod::thisMonth();
Check Overlaps (>=1.4)
$a = CPeriod::make('2021-01-01', '2021-02-01');
$b = CPeriod::make('2021-02-01', '2021-02-28');
cdbg::d($a->overlapsWith($b)); // true
$a = CPeriod::make('2021-01-01', '2021-01-31');
Period Length (>=1.4)
cdbg::d($a->length()); // 31
### Period Boundaries (>=1.4)
$a = CPeriod::make('2021-01-01', '2021-02-01', CPeriod_Precision::DAY(), CPeriod_Boundaries::EXCLUDE_END());
$b = CPeriod::make('2021-02-01', '2021-02-28', CPeriod_Precision::DAY(), CPeriod_Boundaries::EXCLUDE_END());
cdbg::d($a->overlapsWith($b)); // false
Period Array Iterator (>=1.4)
$datePeriod = CPeriod::make(CCarbon::make('2021-01-01'), CCarbon::make('2021-01-31'));
foreach ($datePeriod as $date) {
/** @var DateTimeImmutable $date */
cdbg::d($date);
}
Opening Hours
$data = [
'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' => [],
'exceptions' => [
'2016-11-11' => ['09:00-12:00'],
'2016-12-25' => [],
'01-01' => [], // Recurring on each 1st of January
'12-25' => ['09:00-12:00'], // Recurring on each 25th of December
],
];
$openingHours = CPeriod::openingHours($data);
Current Open Range
$now = new DateTime('now'); // now is 8 June 2023 12:55 pm
$range = $openingHours->currentOpenRange($now);
if ($range) {
echo "It's open since " . $range->start() . "\n<br/>";
echo 'It will close at ' . $range->end() . "\n<br/>";
} else {
echo "It's closed since " . $openingHours->previousClose($now)->format('l H:i') . "\n<br/>";
echo 'It will re-open at ' . $openingHours->nextOpen($now)->format('l H:i') . "\n<br/>";
}
Current Open Range
It's closed since Thursday 12:00
It will re-open at Thursday 13:00
Open on Mondays
$openingHours->isOpenOn('monday')
Open on Mondays: true
Open on Sundays
$openingHours->isOpenOn('sunday')
Open on Sundays: false
Closed because it's after hours
$openingHours->isOpenAt(new DateTime('2016-09-26 19:00:00')) // false
Closed because it's after hours: false
Closed because Christmas was set as an exception
$openingHours->isOpenOn('2016-12-25') // false
Closed because Christmas was set as an exception: false
OpeningHoursForDay object for the regular schedule
$openingHours->forDay('monday') // CPeriod_OpeningHours_OpeningHoursForDay
OpeningHoursForDay[] for the regular schedule, keyed by day name
$openingHours->forWeek(); //array<string,CPeriod_OpeningHours_OpeningHoursForDay>
Array of day with same schedule for the regular schedule, keyed by day name, days combined by working hours
$openingHours->forWeekCombined(); //array<string,CPeriod_OpeningHours_OpeningHoursForDay>
OpeningHoursForDay object for a specific day
$openingHours->forDate(new DateTime('2016-12-25')); //CPeriod_OpeningHours_OpeningHoursForDay
OpeningHoursForDay[] of all exceptions, keyed by date
$openingHours->exceptions(); //array<string,CPeriod_OpeningHours_OpeningHoursForDay>