1. Go to this page and download the library: Download spatie/opening-hours library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
spatie / opening-hours example snippets
// Add the use at the top of each file where you want to use the OpeningHours class:
use Spatie\OpeningHours\OpeningHours;
$openingHours = OpeningHours::create([
'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
],
]);
// This will allow you to display things like:
$now = new DateTime('now');
$range = $openingHours->currentOpenRange($now);
if ($range) {
echo "It's open since ".$range->start()."\n";
echo "It will close at ".$range->end()."\n";
} else {
echo "It's closed since ".$openingHours->previousClose($now)->format('l H:i')."\n";
echo "It will re-open at ".$openingHours->nextOpen($now)->format('l H:i')."\n";
}
// Open on Mondays:
$openingHours->isOpenOn('monday'); // true
// Closed on Sundays:
$openingHours->isOpenOn('sunday'); // false
// Closed because it's after hours:
$openingHours->isOpenAt(new DateTime('2016-09-26 19:00:00')); // false
// Closed because Christmas was set as an exception
$openingHours->isOpenOn('2016-12-25'); // false
// OpeningHoursForDay object for the regular schedule
$openingHours->forDay('monday');
// OpeningHoursForDay[] for the regular schedule, keyed by day name
$openingHours->forWeek();
// Array of day with same schedule for the regular schedule, keyed by day name, days combined by working hours
$openingHours->forWeekCombined();
// OpeningHoursForDay object for a specific day
$openingHours->forDate(new DateTime('2016-12-25'));
// OpeningHoursForDay[] of all exceptions, keyed by date
$openingHours->exceptions();
$openingHours = OpeningHours::create([
'monday' => [
'hours' => [
'09:00-12:00',
'13:00-18:00',
],
'data' => 'Typical Monday',
],
'tuesday' => [
['hours' => '09:00-12:00'],
['hours' => '13:00-18:00'],
['hours' => '19:00-21:00', 'data' => 'Extra on Tuesday evening'],
],
// Open by night from Wednesday 22h to Thursday 7h:
'wednesday' => ['22:00-24:00'], // use the special "24:00" to reach midnight
$openingHours = OpeningHours::create([
'monday to friday' => ['09:00-19:00'],
'saturday to sunday' => [],
'exceptions' => [
// Every year
'12-24 to 12-26' => [
'hours' => [],
'data' => 'Holidays',
],
// Only happening in 2024
'2024-06-25 to 2024-07-01' => [
'hours' => [],
'data' => 'Closed for works',
],
],
]);
$openingHours = OpeningHours::create([
'monday' => [
'09:00-12:00',
],
'filters' => [
function ($date) {
$year = intval($date->format('Y'));
$easterMonday = new DateTimeImmutable('2018-03-21 +'.(easter_days($year) + 1).'days');
if ($date->format('m-d') === $easterMonday->format('m-d')) {
return []; // Closed on Easter Monday
// Any valid exception-array can be returned here (range of hours, with or without data)
}
// Else the filter does not apply to the given date
},
],
]);
// The next open datetime is tomorrow morning, because we’re closed on 25th of December.
$nextOpen = $openingHours->nextOpen(new DateTime('2016-12-25 10:00:00')); // 2016-12-26 09:00:00
// The next open datetime is this afternoon, after the lunch break.
$nextOpen = $openingHours->nextOpen(new DateTime('2016-12-24 11:00:00')); // 2016-12-24 13:00:00
// The next close datetime is at noon.
$nextClose = $openingHours->nextClose(new DateTime('2016-12-24 10:00:00')); // 2016-12-24 12:00:00
// The next close datetime is tomorrow at noon, because we’re closed on 25th of December.
$nextClose = $openingHours->nextClose(new DateTime('2016-12-25 15:00:00')); // 2016-12-26 12:00:00
$ranges = [
'monday' => ['08:00-11:00', '10:00-12:00'],
];
$mergedRanges = OpeningHours::mergeOverlappingRanges($ranges); // Monday becomes ['08:00-12:00']
OpeningHours::create($mergedRanges);
// Or use the following shortcut to create from ranges that possibly overlap:
OpeningHours::createAndMergeOverlappingRanges($ranges);
$openingHours->diffInOpenHours(new DateTime('2016-12-24 11:00:00'), new DateTime('2016-12-24 16:34:25'));
$openingHours->diffInClosedHours(new DateTime('2016-12-24 11:00:00'), new DateTime('2016-12-24 16:34:25'));
$range = $openingHours->currentOpenRange(new DateTime('2016-12-24 11:00:00'));
if ($range) {
echo "It's open since ".$range->start()."\n";
echo "It will close at ".$range->end()."\n";
} else {
echo "It's closed";
}
$period = $openingHours->currentOpenRange(new DateTime('2016-12-24 11:00:00'));
if ($period) {
echo "It's open since ".$period->start()->format('D G\h')."\n";
echo "It will close at ".$period->end()->format('D G\h')."\n";
} else {
echo "It's closed";
}
$date = $openingHours->currentOpenRangeStart(new DateTime('2016-12-24 11:00:00'));
if ($date) {
echo "It's open since ".$date->format('H:i');
} else {
echo "It's closed";
}
$date = $openingHours->currentOpenRangeEnd(new DateTime('2016-12-24 11:00:00'));
if ($date) {
echo "It will close at ".$date->format('H:i');
} else {
echo "It's closed";
}
$openingHours->asStructuredData();
$openingHours->asStructuredData('H:i:s'); // Customize time format, could be 'h:i a', 'G:i', etc.
$openingHours->asStructuredData('H:iP', '-05:00'); // Add a timezone
// Timezone can be numeric or string like "America/Toronto" or a DateTimeZone instance
// But be careful, the time is arbitrary applied on 1970-01-01, so it does not handle daylight
// saving time, meaning Europe/Paris is always +01:00 even in summer time.
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.