我有这个预订表
start_date|end_date
2020-12-15 2020-12-18
2020-12-19 2020-12-22
在这个表中,预订数据正在被保存,但我想在保存数据之前验证日期,如果开始日期和结束日期在数据库日期之间,那么该记录就不应该被保存。
我正在使用这个查询:
$start_date = date('Y-m-d H:i:s', strtotime($request->start_date));
$end_date=date("Y-m-d H:i:s",strtotime('+17 hours', strtotime($request->start_date)));
$bookingData =Booking::whereBetween('start_date', [
$start_date, $end_date
])->whereBetween('end_date', [
$start_date, $end_date
])->exists();
非常感谢您的帮助。
$exists = Booking::whereBetween('start_date', [
$request->start_date, $request->end
])->whereBetween('end_date', [
$request->start_date, $request->end
])->exists();
如果任何预定的预订已经在给定的日期范围内预订,您可以使用以下代码进行检查
DB::select("SELECT * FROM booking ". " WHERE (('$request->start_date' BETWEEN start_date AND end_date)". " OR ('$request->end_date' BETWEEN start_date AND end_date)". " OR (start_date BETWEEN '$request->start_date' AND '$request->end_date'))");