【问题标题】:Compare dateTime to date in laravel在laravel中比较dateTime到日期
【发布时间】:2019-07-15 16:26:47
【问题描述】:

正如标题中所说,我想将日期与 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';
}

如果提交日期小于保修到期打印 Y 否则 N

在我的刀片文件中:

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

但我收到此错误:

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

我做错了什么?

【问题讨论】:

  • 什么是var_dump($this-&gt;date_filed, $this-&gt;warranty_expiration);
  • [date_filed] => 2019-08-28 , [warranty_expiration] => 2019-07-15
  • 数据是这样出来的,作为一个数组吗?那么听起来你需要使用$this-&gt;date_filed['date_filed']
  • 不。我明白这就是我所缺乏的。我会试试这个谢谢你

标签: php laravel eloquent php-carbon


【解决方案1】:

你可以的

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

然后使用等于/更高或等于/更低进行正确比较

您还可以使用“setAttribute”来添加您想要的任何内容,以防保修是/否

【讨论】:

    【解决方案2】:

    变化:

    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

    希望对你有所帮助。

    【讨论】:

    • 你修好了吗? @allenconbulan :)
    猜你喜欢
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 2020-05-31
    • 2021-01-19
    • 2016-08-09
    • 2016-02-21
    • 2015-12-01
    • 1970-01-01
    相关资源
    最近更新 更多