首页 > 解决方案 > 将 startDatetime 和 endDatetime 与 DB 表的 startDatetime 和 endDatetime 进行比较

问题描述

我正在为物业预订创建脚本。如果用户想从 2020-04-23 02:15:33 到 2020-04-23 04:15:33 预订物业,并且已经有另一个人从 2020-04-23 01:01:33 到 2020- 04-23 04:00:00 那么它必须是显示属性已经预订。

    $user_start_date = $this->put('start_date');
    $user_end_date = $this->put('end_date');

    $allDates = new DatePeriod(
                new DateTime($user_start_date),
                new DateInterval('P1D'),
                new DateTime($user_end_date.' +1 day')
   );

   $reqDates = [];
    foreach ($allDates as $date) {
        $reqDates[] = $date->format('Y-m-d H:i:s');
    }

    $bookedDates = $this->db->select('start_date,end_date')
                       ->where('status',1)
                       ->where('property_id', $this->put('property_id'))
                       ->group_start()
                       ->where('start_date >=', $user_start_date)
                       ->or_where('end_date >=', $user_start_date)
                       ->group_end()
                       ->get('property_bookings')->result();
    $flag = 1;   
    foreach($bookedDates as $row) {
        $bookingDates = $allDates = new DatePeriod(
                new DateTime($row->start_date),
                new DateInterval('P1D'),
                new DateTime($row->end_date.' +1 day')
        );
        $bookingDatesArr = [];
        foreach ($allDates as $date) {
            $bookingDatesArr[] = $date->format('Y-m-d H:i:s');
        }

        if(!empty(array_intersect($reqDates,$bookingDatesArr))){
            $data = array( 'status' => "FALSE",
                        'message' => "Property already booked in selected dates.");
            $http_code = REST_Controller::HTTP_INTERNAL_SERVER_ERROR;
            $this->response($data, $http_code);
            $flag = 0;
            exit();
        }
    }

    $insertData = array(
        'property_id' => $this->put('property_id'),
        'house_id' => $this->put('house_id'),
        'start_date' => $this->put('start_date'),
        'end_date' => $this->put('end_date')
    );
    if($flag){
        $insert = $this->db->insert('property_bookings', $insertData); 
    }

标签: phpmysqlcodeigniterdatetime

解决方案


推荐阅读