PHP - Find next available time slot
I'm looking for help in writing a script to fi开发者_开发百科nd the next available time slot.
Ex: Person A wants an appointment and the available time slots everyday is 9am, 12pm, 4pm, 6pm, 9pm
So, I need to check existing data and compare the time slots booked for the other appointments and then find next available.
$apts = array(
'Person 1' => '1/1/09-9:00',
'Person 2' => '1/1/09-12:00',
'Person 3' => '1/1/09-18:00',
'Person 4' => '1/1/09-21:00'
);
So, I need to find next time slot for Person A against $apts, and it should come up with 4pm (1/1/09-16:00). Then comes along Person B and the next time slot for him/her should be '1/2/09-9:00', since no more time slots on 1/1/09, so go to 9am next day.
Make since? Thanks!!
A database would be the best solution semantically speaking. If you want to use one table with columns time
and person_id
, where time
is encoded as UNIX time (easily adaptable to other formats though), you might just want to run:
SELECT time FROM table WHERE person_id = 0 AND time > 1234567890 LIMIT 1;
Keeping to your original PHP only solution, a semantically sensible solution might just be to run a while loop to search for the next available time slot.
$timeslot = $desired_timeslot;
while(in_array($timeslot, $apts)) {
$timeslot = get_next_timeslot($timeslot);
}
return $timeslot;
Of course, you'll have to define the function get_next_timeslot()
to suit your encoding/organizing method.
精彩评论