比较Laravel中的dateTime到日期

时间:2019-07-15 05:25:11

标签: php laravel eloquent php-carbon

正如标题中所述,我想与laravel比较日期。

这是我到目前为止所做的事情:

if (isset($this->date_filed) && !empty($this->date_filed) && !empty($this->warranty_expiration)) {
    $warranty_expiration = Carbon::createFromFormat('m-d-Y', $this->warranty_expiration);
    $date_filed = Carbon::createFromFormat('m-d-Y', $this->date_filed);
    $this->with_warranty = $date_filed->lt($warranty_expiration) ? 'Y' : 'N';
}

如果提交的日期少于保修到期打印日期,否则为N

在我的刀片文件中:

<div class="form-group">
     <label class="form-control-label"><b>Within Warranty:</b></label>
     <p>{{$model->with_warranty}}</p>
</div>

但是我得到这个错误:

  

找不到分隔符号\ r \ n
      追踪数据

我做错了什么?

2 个答案:

答案 0 :(得分:1)

你可以做

$warranty_expiration = date('m-d-Y', strtotime($this->warranty_expiration))
$date_filed = date('m-d-Y', strtotime($this->date_filed))

然后使用等号/大号或等号/小号进行正确比较

如果是/否,您还可以使用“ setAttribute”添加所需的内容

答案 1 :(得分:1)

更改:

if (isset($this->date_filed) && !empty($this->date_filed) && !empty($this->warranty_expiration)) {
    $warranty_expiration = Carbon::createFromFormat('m-d-Y', $this->warranty_expiration);
    $date_filed = Carbon::createFromFormat('m-d-Y', $this->date_filed);
    $this->with_warranty = $date_filed->lt($warranty_expiration) ? 'Y' : 'N';
}

收件人:

if (isset($this->date_filed) && !empty($this->date_filed) && !empty($this->warranty_expiration)) {
    $warranty_expiration = Carbon::parse($this->warranty_expiration)->format('m-d-Y');
    $date_filed = Carbon::parse($this->date_filed)->format('m-d-Y');
    $this->with_warranty = $date_filed->lt($warranty_expiration) ? 'Y' : 'N';
}

查看更多:https://carbon.nesbot.com/docs/#api-comparison

希望它会对您有所帮助。