26.9 C
Pakistan
Saturday, July 27, 2024

Create and Present a PHP Schedule of Occurrence

Spatie’s PHP package Opening Hours allows you to query and format a list of business working hours. It can be used to display the timings for each day and contain exceptions for events such as holidays and other days off on a specified date each year or recurring (occurring on a regular basis). The project’s readme file provides the following example to show you how to set the hours:

$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";
}

This package may be used without any dependencies on any PHP project and comes with a tonne of configurable possibilities. Even if this package isn’t brand-new, we’ve never talked about it before and we think you should give it a look! Spatie Opening Hours is where you can view more API examples and setup instructions.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles