如何按天减去DATE

时间:2017-10-07 11:42:54

标签: php laravel-5 controller

在我的控制器中

$ldate = date('Y-m-d ');
$b =  DB::table('warehouse_products_sell')
               ->select([
                   'warehouse_products_sell.dueto_date'
                ])
               ->get();

我的输出

$b->dueto_date = 2017-10-26

$ldate = 2017-10-07

我想检查

if ($b->dueto_date  - $ldate < 5 ){
 alert('hi')
}

注意:5(天)

不知道该怎么做。

3 个答案:

答案 0 :(得分:2)

Laravel使用Carbon库,因此您可以轻松地依赖它的酷API,

这是一个简单的例子:

use Carbon\Carbon;

$dt1 = Carbon::createFromFormat('Y-m-d', $b->dueto_date);
$dt2 = Carbon::createFromFormat('Y-m-d', $ldate);

$diff = $dt1->diffInDays($dt2);
if ($diff < 5) {
    echo "hi";
}

答案 1 :(得分:2)

$DueDate = new DateTime($b->dueto_date); // Due Date
$ldate   = new DateTime('Y-m-d') // Your date

$interval =  $DueDate->diff($ldate);

if($interval->d < 5){
    echo '<script>alert("less than 5 days remaining")</script>';
}

答案 2 :(得分:1)

如下所示:

$days = floor((strtotime($b->dueto_date)- strtotime($your_date))/ (60 * 60 * 24));
if ($days < 5 ){
  echo "less than 5 days remaining"; //it's php not jQuery
}else{
  echo "$days days remaining";
}

示例示例:https://eval.in/875753