检查日期是否在 Laravel 5 中的日期范围之间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31371822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:56:02  来源:igfitidea点击:

Checking dates if between range of dates in Laravel 5

phplaravellaravel-5eloquent

提问by shaNnex

I have a bookedtable with startand endcolumn as datestamps;

我有一个bookedstartend列作为日期戳;

I know that this

我知道这

$start = "2015-07-12 01:00:00";
$end = "2015-07-12 02:00:00";

$users = DB::table('booked')
                    ->whereBetween('start', [$start, $end])->get();

checks for any dates in startcolumn that falls within $start and $end.

检查start列中位于 $start 和 $end 范围内的任何日期。

What I want is actually the other way.

我想要的其实是另一种方式。

Am having difficulty checking for $start and $end date variables occurrences in startand endcolumns in booktable.

我很难检查 $start 和 $end 日期变量在表中的出现startendbook

回答by Jarek Tkaczyk

// make sure $from is before $till, because they are probably provided as input
$from = min($start, $end);
$till = max($start, $end);

// then simply
DB::table('booked')
     ->where('start', '<=', $from)
     ->where('end', '>=', $till)
     ->get();

It will return rows matching $from-$tillperiod contained in start-endperiod:

它将返回与$from-$tillperiod 中包含的start-endperiod匹配的行:

    start                     end
      |------------------------|
         |----------------|
       $from            $till

回答by Er Parwinder Hirkewal

Please Try under given code this will check is requested dates time are already book or not, if any time slot is booked then it will return 1 otherwise 0 :

请在给定的代码下尝试这将检查请求的日期时间是否已经预订,如果预订了任何时间段,则返回 1 否则返回 0 :

$start = $request->start_date; 
$end = $request->end_date; 
DB::table('booked')->where(function ($query) use ($start, $end) {

    $query->where(function ($q) use ($start, $end) {
        $q->where('start', '>=', $start)
           ->where('start', '<', $end);

    })->orWhere(function ($q) use ($start, $end) {
        $q->where('start', '<=', $start)
           ->where('end', '>', $end);

    })->orWhere(function ($q) use ($start, $end) {
        $q->where('end', '>', $start)
           ->where('end', '<=', $end);

    })->orWhere(function ($q) use ($start, $end) {
        $q->where('start', '>=', $start)
           ->where('end', '<=', $end);
    });

})->count();

回答by Saad Mateen

I resolved my issue with this code, in my case, I have multiple blocked dates, in which we can't allow user to register a new booking, so we have to check the following 12 Wheres:

我用这个代码解决了我的问题,在我的例子中,我有多个被阻止的日期,我们不能允许用户注册新的预订,所以我们必须检查以下 12 个地点:

$start_date = $request->start_date; 
$end_date = $request->end_date; 
DB::table('booked')->where(function ($query) use ($start_date, $end_date) {
    $query->where(function ($q) use ($start_date, $end_date) {
        $q->where('start_date', '>=', $start_date)
            ->where('start_date', '=<', $start_date);
    })->orWhere(function ($q) use ($start_date, $end_date) {
        $q->where('end_date', '>=', $start_date)
            ->where('end_date', '<=', $start_date);
    })->orWhere(function ($q) use ($start_date, $end_date) {
        $q->where('start_date', '>=', $end_date)
            ->where('start_date', '<=', $end_date);
    })->orWhere(function ($q) use ($start_date, $end_date) {
        $q->where('end_date', '>=', $end_date)
            ->where('end_date', '<=', $end_date);
    })->orWhere(function ($q) use ($start_date, $end_date) {
        $q->where('start_date', '<=', $start_date)
            ->where('end_date', '>=', $start_date);
    })->orWhere(function ($q) use ($start_date, $end_date) {
        $q->where('start_date', '<=', $end_date)
            ->where('end_date', '>=', $end_date);
    });
})->count();

回答by jedrzej.kurylo

The following query will give you all records for which both start and end are between $start and $end:

以下查询将为您提供 start 和 end 都在 $start 和 $end 之间的所有记录:

$users = DB::table('booked')
  ->where('start', '>=', $start)
  ->where('start', '<=', $end)
  ->where('end', '>=', $start)
  ->where('end', '<=', $end);