无法在 laravel carbon 中验证真假 var_dump

can't validate true or false var_dump in laravel carbon

我编写代码来检查 $start 和 $end 之间的 2 次当前时间,如下所示:

$current = new Carbon();
$start = product::select('dateS')->where('id',$req->id)->first(); 
$end = product::select('dateE')->where('id',$req->id)->first();
$val= var_dump($current->between($start->dateS,  $end->dateE));

它有效,当我使用 dd($val) 时它会显示:

布尔值(假) null 或 bool(true) null

接下来,我想这样做,但只有 returns 其他情况。我做错了什么?

 if($val == true){
      echo "current time is between start and end";
    }
    else{
      echo "current time isn't between start and end";
    }

您可以得到开始和结束如下:

$start_and_end_time = product::where('id',$req->id)->select(['dateS', 'dateE'])->first();

如果您在 Product 模型中将日期转换为 dateSdateE,您将通过上述查询获得 carbon 实例。

产品型号

dates = [
    'dateS',
    'dateE'
];

因此,您可以将当前日期时间与 dateSdateE 进行比较,如下所示:

$val = \Carbon::now()->between($start_and_end_time->dateS, $start_and_end_time->dateE);

您可以调整您的代码以从作业中删除 var_dump 调用,您应该没问题:

$current = new Carbon();
$product = product::findOrFail($req->id, ['dateS', 'dateE']);

if ($current->between($product->dateS, $product->dateE)) {
    echo "current time is between start and end";
} else {
    echo "current time isn't between start and end";
}

还合并了查询,因为您只需要 1 个查询就可以获得所需的数据。