简体   繁体   English

如何在php的日期范围列表中检查日期范围

[英]How to check a date range in list of date range in php

I have a table offer where I stored multiple date range for a product.我有一个表格报价,我在其中存储了一个产品的多个日期范围。

------------------table----------------------

offer_id | sdate        | edate       | price    
  1      |  5-08-2019   | 10-08-2019  | 200    
  1      |  15-08-2019  | 20-08-2019  | 200

The problem is when I want to store a new date range for offer_id 1 should not match or between a stored date range.问题是当我想为 offer_id 存储一个新的日期范围时, 1 不应该匹配或在存储的日期范围之间。

exmple例子

if I want to store 02-08-2019 to 05-08-2019 should not be saved.如果我想存储 02-08-2019 到 05-08-2019 不应该保存。 either if I want to store 06-08-2019 to 12-08-2019 should not be store because of 06-08-2019 between the first record.如果我想将 06-08-2019 存储到 12-08-2019 不应该存储,因为第一条记录之间是 06-08-2019。

    $saveStartDate = Test::where('offer_id',1)->pluck('sdate');
    $saveEndDate = Test::where('offer_id',1)->pluck('edate');

   
    $startDate = $request->sdate;
    $endeDate = $request->edate;

    // validation

    foreach($saveStartDate as $saveStartValue){
        if($saveStartValue > $startDate ){
            foreach($saveEndDate as $saveEndValue){
                if($saveEndValue > $saveEndDate){
                    return "go to save";
                }
            }
        }
    }
    return "exit";

You could check if the an existing record overlaps with the requested date range:您可以检查现有记录是否与请求的日期范围重叠:

if (Test::whereBetween('sdate', [$startDate, $endDate])
        ->orWhereBetween('edate', [$startDate, $endDate])
        ->exists()) {
    echo('Requested date range overlaps with an existing record');
}

You can try to use Carbon to parse the requested date then check the existing record in DB to determine the two date range if overlap.您可以尝试使用Carbon来解析请求的日期,然后检查数据库中的现有记录以确定两个日期范围是否重叠。

$startDate  = \Carbon\Carbon::parse($request->sdate);
$endeDate = \Carbon\Carbon::parse($request->edate);

$dataCount = Test::whereBetween('sdate', [$startDate, $endeDate])
                  ->orWhereBetween('edate', [$startDate, $endeDate])->count();
if($dataCount > 0){
     return "go to save";
}

I solve my problem but I use more than 1 condition我解决了我的问题,但我使用了 1 个以上的条件

     $data = Test::get();
     $i = Test::where(function ($query) use ($startDate, $endDate) {
        $query->where(function ($query) use ($startDate, $endDate) {
           $query->where('start_date', '>=', $startDate)
                   ->where('end_date', '<', $startDate);
           })
           ->orWhere(function ($query) use ($startDate, $endDate) {
               $query->where('start_date', '<', $endDate)
                       ->where('end_date', '>=', $endDate);
           });
       })->count();

     if($i == 0){
         $check = Test::where('service_detail_id', $id)
        ->whereNotBetween('start_date', [$startDate, $endDate])
        ->where('start_date','>', $startDate)
        ->where('start_date','>' , $endDate )
        ->get();
        if(count($check) > 0 ){
            $i = 0;
        }
        else{
            $i = 1;
        }
     }

     if($i == 0){
        foreach($data as $value){
            if($value->start_date < $startDate && $value->end_date > $startDate ){
                $i =1;
            }else{
                $i =0;
            }
        }
     }

   if($i == 0){
   return "go and store";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM