基于数据库中的时间记录打开/关闭状态

时间:2012-12-16 14:31:10

标签: php mysql codeigniter

根据时间范围,我有一个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。

  

显然,这种转变是 OPEN ,但是基于周日的时间范围,而不是周一的当天。

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

3 个答案:

答案 0 :(得分: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-&gt; _get_current_day()就在我的问题中。

我想必须有更优雅和安全的解决方案。

答案 1 :(得分: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开始。

答案 2 :(得分: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