In this episode, we dive into how to work with date periods, which is super important for building our employee scheduling and availability system. We start by introducing the Sparty period package—if you haven't used it before, it's really powerful and lets you do things like chop up periods, subtract times, and generally manipulate date spans however you need.
First, we set up a new Booking
directory and create a ScheduleAvailability
PHP class, which is where all the fun will happen. Inside this class, we initialize a period collection and start experimenting. We set up a test route so we can quickly see what our code is doing in the browser.
To begin, we create a period that goes from the start of today to the end of tomorrow, just to get a baseline. From there, we show how you can subtract time blocks—like if an employee already has an appointment. When we subtract, say, a lunch break or a meeting from the available period, the collection splits and accurately reflects the new available times!
The takeaway here is that by stacking up these additions and subtractions, we’ll eventually be able to filter employee schedules down to just the periods when they’re actually available. In the next steps, we'll use this base to handle real-life data, take into account staff working hours, existing bookings, and more. Once that groundwork is laid, we’ll be ready to generate and display bookable slots to users.