在laravel中相互比较4个日期

时间:2019-04-22 18:39:23

标签: php laravel

考虑到我有2个用户发送给我的日期,如下所示:

$from_date = $request->get('from_date');
$to_date= $request->get('from_date');

在名为“历史记录”的表中,我拥有的列如下所示:

Schema::create('histories', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->daetime('from');
        $table->datetime('to')->unique();
        $table->timestamps();
    });

所以现在我想要的是当用户选择时间范围时,我想检查表上是否存在该时间,是否存在,我想一一比较天,这是到目前为止我已经尝试过的方法:

$period = CarbonPeriod::create($from_date, $to_date);
foreach ($period as $date) {
    echo $date->format('Y-m-d');
}
$dates = $period->toArray();

并简单地获取colomn中的所有日期:

$date = History::all();
foreach($date as $x){
 $db_daterange = $x->from_date 
}

现在为了比较日期,我尝试了这个:

 foreach($dates as $y){
     if($y->isSameDay($db_daterange)){
       dd('same day');
    }
}

好吧,我不确定最后一部分,但这是我通常想要的,如果给定日期存在于数据库日期中,那么我想获取数据库中的所有日期与给定日期进行比较,然后简单地回声但我想搜索表的所有列以及从“ from_date到“ to_date”的所有日期

1 个答案:

答案 0 :(得分:-1)

您可以尝试在whereBetween

Type '{ children: Element; color?: string | undefined; hoverColor?: string | undefined; onMouseEnter?: ((event: MouseEvent<{}, MouseEvent>) => void) | undefined; onMouseLeave?: ((event: MouseEvent<{}, MouseEvent>) => void) | undefined; ... 413 more ...; ref?: string | ... 3 more ... | undefined; }' is not assignable to type 'SvgIconProps'.
  Types of property 'color' are incompatible.
    Type 'string | undefined' is not assignable to type '"inherit" | "default" | "disabled" | "error" | "primary" | "secondary" | "action" | undefined'.
      Type 'string' is not assignable to type '"inherit" | "default" | "disabled" | "error" | "primary" | "secondary" | "action" | undefined'.