使用Laravel比较两个数组

3
大家好,我有一个数组。
$dates = [2016-11-02,2016-11-05,2016-11-11,2016-11-15,2016-11-16]

我现在获得了所有日期,我想将它们与$currentdate (2016-12-16)进行比较。

if($dates <= $currentdate){
  echo "true"; 
} else{
  echo "false";
}

任何帮助都将受到高度赞赏。
4个回答

1

不确定其他答案,但如果您想验证所有日期,请使用以下代码……应该可以工作:)

$isLess = false;
$currentDate = Carbon::now();

foreach($dates as $data) {
  if(Carbon::parse($date)->lte(currentDate)){
    $isLess = true;
    break;
  }
}

echo $isLess;

如果您遇到任何问题,请在评论中让我知道 :)


我现在有一个 $dates[],想要像这样比较当前日期:$mytime = Carbon::now(); $time = $mytime->toDateString(); - Rahul Singh
@RahulSingh 这将适用于 $dates 数组。而且为什么要将 Carbon::now() 转换为 DateString?这将导致字符串比较... - prateekkathal

0

由于您正在使用Laravel,因此您可以使用Carbon来完成:

if (Carbon::now()->gte(Carbon::parse($oneDate))

我有一个包含日期和当前日期的数组,如何在 if (Carbon::parse($date) < Carbon::now()) 中传递数组?谢谢。 - Rahul Singh
首先迭代您的数组,然后进行比较。 - Alexey Mezenin

0

你可以将其解析为Carbon并使用其比较函数

$date = Carbon::parse($date);
if($date->lte(Carbon::today())){
 // your code
}

这里的lte()表示小于等于

要了解更多关于Carbon比较运算符的信息,请访问http://carbon.nesbot.com/docs/#api-comparison


-1

虽然不是最美观的,但只需一行代码

if (collect(collect($dates)->filter(function($date) use($currentDate) { return Carbon::parse($date)->lte($currentDate); }))->count > 0) {

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接