在 Laravel 中验证日期

Validate Date in Laravel

我有这个预订table

start_date | end_date

2020-12-15   2020-12-18
2020-12-19   2020-12-22

在此table中,正在保存预订数据,但我想在保存数据之前验证日期,如果开始日期和结束日期在数据库日期之间,则不应保存该记录。

我正在使用这个查询:

$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'))");