Created
November 2, 2016 19:41
-
-
Save janizde/baabb1e23e8b68973cb74080bff6c1c3 to your computer and use it in GitHub Desktop.
Get OpeningHours for current day
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use OpeningHours\Module\OpeningHours; | |
use OpeningHours\Entity\IrregularOpening; | |
use OpeningHours\Entity\Period; | |
$openingHours = OpeningHours::getInstance(); | |
$set = $openingHours->getSet(4); // Specify Set id; $set instanceof OpeningHours\Entity\Set | |
$isOpen = $set->isOpen(); | |
if ($isOpen) { | |
$io = $set->getActiveIrregularOpening(); | |
if ($io instanceof IrregularOpening) { | |
// is open due to irregular opening | |
$timeStart = $io->getTimeStart(); // $timeStart instanceof \DateTime | |
$timeEnd = $io->getTimeEnd(); // $timeEnd instanceof \DateTime | |
} else { | |
// is open due to regular periods | |
$periods = $set->getPeriods()->getArrayCopy(); // $periods: OpeningHours\Entity\Period[] | |
$today = (int) date('w', time()); // integer 0 - 6 | |
$todaysPeriods = array_filter($periods, function (Period $p) use ($today) { | |
return $p->getWeekday() === $today; | |
}); | |
// $todayPeriods only containds Periods on today's weekday | |
echo 'We\'re open today: '; | |
foreach ($todayPeriods as $p) { | |
echo $p->getFormattedTimeRange(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment