doudouji2016 2019-03-27 05:30
浏览 81

如何使用php中的时间段获取下一个7天的数据

I am working with api's in codeigniter,I have two tables and i want to get data of next 7 days with 30 minutes slot(whether shop is booked or availiable)

my first table is "additional_info"

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

second table is "booking"

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

now i want time slot of shop id (start with his open time to close time ) with every 30 minutes slot (whether time is booked or not)

in other words,Now i want result like

{
    "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
                ...

And i tried with following code,but get wrong data,i am getting the data of that date which is stored in database,but i want to check with current date to next 6 days,here is my code

$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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 BP神经网络控制倒立摆
    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算