I was wondering what the best approach would be to validate a date is within the correct opening hours of a shop…
I am creating a website for a golf professional and am taking bookings for lessons on it. I have used fullCalendar for the calendar and integrated it with google calendar so it can be easily sync’d to the pro’s mobile easily etc.
As part of the calendar, when a customer clicks on a time slot I am provided with a date object by fullcalendar. I want to validate that this selected slot falls within the opening times of the shop for lessons to be taken etc.
The times will be retrieved from a db table at some point but I can have them returned in JSON onto the page if it works best – but thats not the purpose of this question…
The opening hours differ depending on the day of the week as well as the “season” [summer times -vs- winter times]
ie:
Summer Times
Monday = 10:00 - 21:00
Tuesday = 10:00 - 21:00
Wednesday = 10:00 - 21:00
Thursday = 10:00 - 21:00
Friday = 10:00 - 21:00
Saturday = 08:00 - 18:00
Sunday = 08:00 - 18:00
Winter Times
Monday = CLOSED
Tuesday = 10:00 - 18:30
Wednesday = 10:00 - 18:30
Thursday = 12.00 – 20.30
Friday = 12.00 – 20.30
Saturday = 08:00 - 18:00
Sunday = 08:00 - 18:00
What would the “best” way be to validate this?
would the best option be to simply do nested if/switch statements..
[sudo code]
if(summer}
{
switch(date.getDay())
{
case 1:
if(time > mondayOpeningTime && time < mondayCloseTime)
{
return true;
}
else
{
return false;
}
break;
case 2:
execute code block 2
break;
.....
default:
code to be executed if n is different from case 1 and 2
}
}
else
{
// Same as summer logic but for winter times etc..
}
Or is there a better way of doing this?
Apologies for the long post/question but thanks in advance to anyone who spends the time reading and responding to it 🙂
Chris
You want 2 arrays: openingTimes and closingTimes. First dimension will be season, second dimension will be day of the week. Then it’s just: