Я создаю скрипт для бронирования недвижимости. если пользователь хочет забронировать номер с 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);
}