php - 将 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);
}
解决方案
推荐阅读
- swift - SwiftUI onTapGesture 不适用于 Mac 应用程序中的 ObservedObject
- sirishortcuts - 查找笔记问题
- ios - 在 tabbedView 中实现时,状态栏颜色不会随 navigationBar 改变
- python - 我怎样才能输入一些标准值,然后继续
- reactjs - 动态生成 className 时,如何更改样式化组件主题?
- javascript - 无法使用 Sinon 模拟节点 fs 模块
- core-data - SwiftUI / CoreData / Master / Detail(带编辑)/ Xcode 11 - Beta 5
- android - Android Intent Filters - 专有文件类型的空白 mimeType
- javascript - Firebase 列表未在运行时显示
- azure - 在 Azure Active Directory 中预授权应用程序:对其他应用程序隐藏范围