1

我有一个 mySQL 表,用于根据时间范围指示商店开店或关店的天气:

 shift table
--------------
 shift_id
 day_of_week    (1-7 : Monday-Sunday)
 open_time      (time, default NULL)
 close_time     (time, default NULL)
 type           (1 || 2)

字段类型说明: 1表示同一天内的第一个时间帧,2表示该时间帧是同一天内的第二个时间帧。例如商店在 13:00 - 15:00 和 18:00 - 01:00 营业。

我正在使用以下方法获取当前日期:

$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;

我正在使用以下方法获取当前时间:

$timeString = "%H:%i:%s";
$curTime = mdate($timeString, time());

复杂示例如下
当前日期:星期一
当前时间:02:00。
周日时间范围:15:00 - 18:00 和 21:00 - 02:30。
周一时间范围:08:30 - 15:30。

显然,班次是开放的,但基于周日的时间范围,而不是在当天的周一。

在给定日期的给定时间,查询我的表(或在需要时更改我的表)的最佳方法或 100% 准确的方法来定义每次班次是打开还是关闭的方法是什么?

4

3 回答 3

0
function shop_open(){
    //Get current day
    $day = $this->_get_current_day();

    //Get current time
    $timeString = "%H:%i:%s";
    $ordertime = mdate($timeString, time());
    //Predefine ability to order to false
    $canOrder = false;

    $getShift = $this->db->query("select * from `shop_hours` where `day_of_week` = $day AND !ISNULL(`open_time`) ORDER BY `type`");
    $day_num_rows = $getShift->num_rows();

    switch($day_num_rows){
        case 0: //No records
            $canOrder = false;
            break;

        case 1: //Only 1 timeframe
            foreach($getShift->result() as $row){
                $open_time = $row->open_time;
                $close_time = $row->close_time;
            }

            if($open_time <= $close_time){//Timeframe is within the same day [no overlap]

                if($open_time < $ordertime && $close_time > $ordertime){//Current time is within timeframe
                    $canOrder = true;
                }else{
                    $canOrder = false;
                }

            }else if($open_time > $close_time){//There s your overlap [timframe "catches" the next day]

                if($open_time < $ordertime && '23:59' >= $ordertime){//Check till midnight first
                    $canOrder = true;
                }else{//Check in the previous day

                    if($day == 1){ $previousDay = 7; }else{ $previousDay = $day-1; }

                    $getPrvDayShift = $this->db->query("select * from `shop_hours` where `day_of_week` = $previousDay ORDER BY `type`");
                    $prv_day_num_rows = $getPrvDayShift->num_rows();

                    switch($prv_day_num_rows){
                        case 0://No records
                            $canOrder = false;
                            break;
                        case 1://One record found
                            foreach($getPrvDayShift->result() as $row2){
                                $prv_close_time = $row2->close_time;
                            }

                            if($prv_close_time > $ordertime){ //So if the previous day close time is greater than current, we are good to go
                                $canOrder = true;
                            }else{
                                $canOrder = false;
                            }

                            break;
                        case 2: //2nd record found
                            foreach($getPrvDayShift->result() as $row2){
                                if($row2->type == 2){
                                    $prv_close_time = $row2->close_time;
                                }
                            }
                            if($prv_close_time > $ordertime){ //Same shit, different timeframe within the same day 
                                $canOrder = true;
                            }else{
                                $canOrder = false;
                            }
                            break;
                    }

                }

            }
            break;

        case 2: //Ok now we have to check the case of 2 timeframes together

            for($i=1; $i<3; $i++){
                foreach($getShift->result() as $row){
                    if($row->type == $i){
                        $open_time = $row->open_time;
                        $close_time = $row->close_time;
                    }
                }
                if($open_time <= $close_time){//Again, same day [no overlap]
                    if($open_time < $ordertime && $close_time > $ordertime){//Good to go we are within timeframe
                        $canOrder = true;
                    }else{
                        $canOrder = false;
                    }
                }else if($open_time > $close_time){//There s your overlap
                    //Check till midnight
                    if($open_time < $ordertime && '23:59' >= $ordertime){ $canOrder = true; }
                }
            }

            if(!$canOrder){
                //Now we should check the previous day also if $canOrder is still negative
                if($day == 1){ $previousDay = 7; }else{ $previousDay = $day-1; }
                $getPrvDayShift = $this->db->query("select * from `shop_hours` where `day_of_week` = $previousDay ORDER BY `type`");
                $prv_day_num_rows = $getPrvDayShift->num_rows();

                switch($prv_day_num_rows){
                    case 0:
                        $canOrder = false;
                        break;
                    case 1:
                        foreach($getPrvDayShift->result() as $row2){
                            $prv_close_time = $row2->close_time;
                        }
                        if($prv_close_time > $ordertime){ $canOrder = true; }else{ $canOrder = false; }

                        break;
                    case 2:
                        foreach($getPrvDayShift->result() as $row2){
                            if($row2->type == 2){
                                $prv_close_time = $row2->close_time;
                            }
                        }
                        if($prv_close_time > $ordertime){ $canOrder = true; }else{ $canOrder = false; }
                        break;
                }
            }

            break;
    }
    return $canOrder;
}

$this->_get_current_day() 在我的问题中。

我想一定有一个更优雅和安全的解决方案。

于 2012-12-16T18:12:13.863 回答
0

这就是我的想法:

CREATE TABLE Shifts (
  shift_id INT AUTO_INCREMENT PRIMARY KEY,
  start_day CHAR(9) NOT NULL,
  start_time TIME NOT NULL,
  end_day CHAR(9) NOT NULL,
  end_time TIME NOT NULL
) ENGINE = InnoDB;

现在我想起来了,我不确定你甚至需要end_day. 但是,使用:

INSERT INTO Shifts (
  start_day,
  start_time,
  end_day,
  end_time
) VALUES
( LOWER('Sunday'), '15:00:00', LOWER('Sunday'), '18:30:00' ),
( LOWER('Sunday'), '21:00:00', LOWER('Monday'), '26:30:00' ),
( LOWER('Monday'), '12:00:00', LOWER('Monday'), '18:00:00' ),
( LOWER('Tuesday'), '10:00:00', LOWER('Tuesday'), '20:45:00' ),
( LOWER('Wednesday'), '16:00:00', LOWER('Wednesday'), '19:30:00' ),
( LOWER('Thursday'), '10:00:00', LOWER('Thursday'), '17:00:00' ),
( LOWER('Thursday'), '19:00:00', LOWER('Friday'), '25:30:00' ),
( LOWER('Friday'), '16:00:00', LOWER('Saturday'), '24:30:00' ),
( LOWER('Saturday'), '15:00:00', LOWER('Saturday'), '20:00:00' ),
( LOWER('Saturday'), '18:30:00', LOWER('Sunday'), '27:00:00' )

然后,您可以使用以下内容:

SELECT DAYNAME(NOW()), start_day
FROM Shifts
WHERE (start_day = LOWER(DAYNAME(NOW()))
       AND start_time < CURTIME()
       AND end_time > CURTIME())
   OR (start_day = LOWER(DAYNAME(DATE_SUB(NOW(), INTERVAL 1 DAY)))
       AND start_time < ADDTIME('24:00:00', CURTIME())
       AND end_time > ADDTIME('24:00:00', CURTIME()))

你可以在这里测试:

http://sqlfiddle.com/#!2/41a26/34

并尝试使用不同的测试值:

SELECT DAYNAME(NOW()), start_day
FROM Shifts
WHERE (start_day = LOWER('Friday')
       AND start_time < '01:12:35'
       AND end_time > '01:12:35')
   OR (start_day = LOWER('Thursday')
       AND start_time < ADDTIME('24:00:00', '01:12:35')
       AND end_time > ADDTIME('24:00:00', '01:12:35'))

http://sqlfiddle.com/#!2/41a26/33

于 2012-12-20T22:10:32.220 回答
0

为了区分延伸到第二天的时间范围和没有延伸到第二天的时间范围,任何时间范围都不能超过 24 小时。因此,这些时间框架的收盘时间必须小于或等于开盘时间。

然后,此查询应在给定时间找到打开的班次:

SELECT shift_id
FROM shifts
WHERE :curDay = day_of_week   
AND :curTime >= open_time                                -- today
AND (:curTime <= close_time OR close_time <= open_time)  -- shift may end tomorrow
OR :curDay = IF(day_of_week=1,7,day_of_week-1)           -- check yesterday
AND :curTime <= close_time AND close_time <= open_time   -- shift started yesterday

如果可能的话,我会更改表格,将跨越午夜的班次分成两个班次,一个在 24:00 结束,另一个从第二天 00:00 开始。

于 2012-12-16T21:27:46.210 回答