如何在php中获取带有时隙的未来7天数据

时间:2019-03-27 05:30:07

标签: php codeigniter

我正在codeigniter中使用api's,我有两个表,我想在30分钟内获取接下来7天的数据(无论商店是否已预订)

我的第一个表是“ additional_info”

id  shop_id     shop_open_time  shop_close_time
1   3           09:00:00        22:00:00

第二张表是“预订”

id      user_id     shop_id     service_id      duration(in minutes)    services_dates      start_time      end_time
1       2           3           1               20                      2019-03-26          09:01:10        10:00:00

现在我希望每隔30分钟(无论是否预定时间)有一个商店ID的时隙(以他的营业时间开始,以关闭时间为准)

换句话说,现在我想要类似的结果

{
    "Status": true,
    "Result": {
        "id": "4",
        "salon_id": "3",
        "shop_open_time": "09:00:00.000000",
        "shop_close_time": "22:00:00.000000",
       "availability": [
            {
                "id": 1,
                "date": "27-Mar-2019",
                "arrTimeSlots": [
                    {
                        "id": 1,
                        "time": "09:00 AM",
                        "alreadyBooked": false
                    },
                    {
                        "id": 2,
                        "time": "09:30 AM",
                        "alreadyBooked": false
                    },
                    .....
            {
                "id": 2,
                "date": "28-Mar-2019",
                "arrTimeSlots": [
                    {
                        "id": 1,
                        "time": "09:00 AM",
                        "alreadyBooked": false
                    },
                    {
                        "id": 2,
                        "time": "09:30 AM",
                        "alreadyBooked": false
                    },
                    {
                        "id": 3,
                        "time": "10:00 AM",
                        "alreadyBooked": false
                    },
                    {
                        "id": 4,
                        "time": "10:30 AM",
                        "alreadyBooked": false
                    },
                ....
                //next 5 days
                ...

我尝试使用以下代码,但是得到了错误的数据,我正在获取存储在数据库中的该日期的数据,但是我想检查当前日期到未来6天的时间,这是我的代码

$add_data['shop_id'] = ($this->input->post('shop_id') && !empty($this->input->post('shop_id'))) ? $this->input->post('shop_id') : NULL;     

                $this->db->select('*');
                $this->db->from('additional_info');
                $this->db->where('shop_id',$add_data['shop_id']);
                $query = $this->db->get();
                if ( $query->num_rows() > 0 )
                    {
                        $row = $query->row_array();
                         $start=$row['shop_open_time'];
                         $end=$row['shop_close_time'];
                        $start_time=substr($start, 0, -10);
                        $end_time=substr($end, 0, -10);
                        $start_time = new DateTime($start_time);
                        $close_time = new DateTime($end_time);
                        $periods = new DatePeriod($start_time, new DateInterval('PT30M'), $close_time);
                        $availability_time = array();
                        $i=0;
                        foreach ($periods as $period) {
                            $availability_time[] = array('id'=>++$i, 'time'=>$period->format('h:i A'), 'alreadyBooked'=>false);
                        }
                        $now = new DateTime();
                        $startDate = date('d-M-Y');
                        $endDate = $now->add(new DateInterval('P1W'))->format('d-M-Y');

                        $availability = array();
                        //To find dates between two dates as an array
                        $dates = new DatePeriod(
                            new DateTime($startDate), new DateInterval('P1D'), new DateTime($endDate)
                        );
                        $ii = 0;
                        foreach ($dates as $dt) {
                           $availability[] = array('id'=>++$ii, 'date'=>$dt->format('d-M-Y'),'arrTimeSlots'=>$availability_time, 'isSelected'=>false);
                        }
                        $availability_time = array_fill_keys($times, 'available');


function book(&$availability_time, $start, $end)
                        {
                           // $i=0;
                            //foreach($availability_time as $k => $v)
                             //   $availability['id']=++$i;

                            //  if(strtotime($k) >= strtotime($start) && strtotime($k) <= strtotime($end))

                            //      $availability_time = 'booked';
                        }                       

                $this->db->select('*');
                $this->db->from('usr_booking');
                $this->db->where('shop_id',$add_data['shop_id']);
                $queryc = $this->db->get();
                foreach($queryc->result_array() as $results)
                    {
                        //print_R($results);            
                        $s=$results['start_time'];
                        $e=$results['end_time'];
                    }
                $records = $query->result_array();              
                $newrecord=$records['0'];
                $newrecord['availability']=$availability;
                return  $newrecord;
                    }   
                else
                    {
                        return false;
                    }

0 个答案:

没有答案