I have a mySQL table to indicate weather the shop is open or closed, based on time frames:
shift table
--------------
shift_id
day_of_week (1-7 : Monday-Sunday)
open_time (time, default NULL)
close_time (time, default NULL)
type (1 || 2)
Explanation of field type:
1 indicates the first timeframe within the same day and 2 indicates that the timeframe is the second timeframe within the same day.
For example shop opens on 13:00 – 15:00 AND 18:00 – 01:00.
I am getting the current day using the method below:
$jd = cal_to_jd(CAL_GREGORIAN,date("m"),date("d"),date("Y"));
$day = jddayofweek($jd, 0);
switch($day){
case 0:
$curDay = 7;
break;
default:
$curDay = $day;
break;
}
return $curDay;
I am getting the current time using the method below:
$timeString = "%H:%i:%s";
$curTime = mdate($timeString, time());
Complex example follows:
Current day: Monday
Current time: 02:00.
Sunday timeframe: 15:00 – 18:00 and 21:00 – 02:30.
Monday timeframe: 08:30 – 15:30.
Clearly the shift is OPEN but based on Sunday s timeframe and not on Monday s which is the current day.
What is the best way to query my table (or alter my table if needed) or the 100% accurate method to define each time if the shift is open or closed, in a given time of a given day ?
This is what I was thinking:
Now that I think about it, I’m not really sure you even need
end_day. However, using:You could then use something like the following:
Which you can test out here:
http://sqlfiddle.com/#!2/41a26/34
And try it with different test values:
http://sqlfiddle.com/#!2/41a26/33