Php 在laravel中相互比较4个日期

Php 在laravel中相互比较4个日期,php,laravel,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');

假设我有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();
    });
在我的名为history的表中,我的列如下所示:

$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();
要获取列中的所有日期,只需:

$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”的所有日期“

你可以用where-between试试

History::whereBetween('from', ['2019-01-01', '2019-03-31'])->get()

您的
histories
表没有
from\u date
,但您可以以
History
属性的形式访问
$x->from\u date