garrettmassey's avatar

Using Spatie/Opening-Hours to determine if a given business is open at a specific time on a given weekday

Hello!

I am working on writing a check system so that a user can enter times they might need service, and then we check the database to see what businesses are open during that time, if any. To help with this, I am using the Spatie Opening Hours PHP package. There is a method built in that lets you see if a business is open on a given day, like so:

$business->isOpenOn('monday')
//returns true or false

and I have added my own method that accepts an array of days like this:

$business->isOpenOnDays(['monday', 'tuesday']);
//returns true if open on ALL days, false if closed on ANY days

It also has a method that lets you check if it is open at a specific dateTime

$business->isOpenAt(new DateTime('2016-26-09 20:00'));
//true if open, false if not

and what I am trying to figure out how to do is this, given a weekday and a time range, return true if the business is open, false if not, like this:

$business->isOpenAtDayTime('monday', ['12:00:00', '13:00:00']);
$business->isOpenAtDayTime(string $day, Array $time);

where the first argument is a string for the day of week, and the second argument is an array with two elements that are time strings.

I'm having a hard time figuring out the best way to go about this. If I check if a given business is open on a day, that works, and I can get the hours of that day, but the package formats the hours in array format like this:

Spatie\OpeningHours\OpeningHours {#5473 ▼
#openingHours: array:7 [▼
	"monday" => Spatie\OpeningHours\OpeningHoursForDay {#5463 ▼
  		#openingHours: array:1 [▼
    		0 => Spatie\OpeningHours\TimeRange {#5464 ▼
      			#start: Spatie\OpeningHours\Time {#5468 ▶}
      			#end: Spatie\OpeningHours\Time {#5444 ▶}
      			#data: null
    }

and the #start and #end look like this:

start: Spatie\OpeningHours\Time {5468 ▼
        #hours: 6
        #minutes: 30
        #data: null
}

I'm not sure what the best approach here is, I've been reading the documentation and the code itself for the package to see if I can extract existing code into a new method, but I'm stumped. Any help is appreciated!

0 likes
0 replies

Please or to participate in this conversation.