Laravel Blade 比较两个日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42469816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel blade compare two date
提问by Christopher
I would like to compare 2 dates. So I create a condition like this in my template blade:
我想比较 2 个日期。所以我在我的模板刀片中创建了这样的条件:
@if(\Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') < $dateNow)
<td class="danger">
{{ \Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') }}
</td>
@else
<td>
{{ \Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') }}
</td>
@endif
My variable $dateNow has the same format on my value in $contract->date_facturation
我的变量 $dateNow 在 $contract->date_facturation 中的值具有相同的格式
It add a red background on the date 25/02/2018 while the date is not less than the variable $contract->date_facturation
它在日期 25/02/2018 上添加红色背景,同时日期不小于变量 $contract->date_facturation
Do you have any idea of ??the problem?
你有什么想法吗??这个问题?
Thank you
谢谢
回答by Elias Soares
The problem is that you are trying to compare two date strings. PHP don't know how to compare date strings.
问题是您正在尝试比较两个日期字符串。PHP 不知道如何比较日期字符串。
Carbon::format()
returns a string. You shoud convert your dates to a Carbon object (using parse) and use Carbon's comparison methods, as described on Carbon Docs (Comparison).
Carbon::format()
返回一个字符串。您应该将日期转换为 Carbon 对象(使用解析)并使用 Carbon 的比较方法,如Carbon Docs (Comparison) 中所述。
For your example, you should do:
对于您的示例,您应该执行以下操作:
// Note that for this work, $dateNow MUST be a carbon instance.
@if(\Carbon\Carbon::parse($contrat->date_facturation)->lt($dateNow))
<td class="danger">
{{ \Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') }}
</td>
@else
<td>
{{ \Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') }}
</td>
@endif
Also your code looks repetitive, and assuming that $dateNow
is a variable with current date, you can use Carbon::isPast()
method, so rewriting your code, it becomes:
此外,您的代码看起来是重复的,假设这$dateNow
是一个具有当前日期的变量,您可以使用Carbon::isPast()
方法,因此重写您的代码,它变成:
@php($date_facturation = \Carbon\Carbon::parse($contrat->date_facturation))
@if ($date_facturation->isPast())
<td class="danger">
@else
<td>
@endif
{{ $date_facturation->format('d/m/Y') }}
</td>
This makes your code less repetitive and faster, since you parse the date once, instead of twice.
这使您的代码更少重复和更快,因为您解析日期一次,而不是两次。
Also if you want your code better, use Eloquent's Date Mutators, so you won't need to parse dates everytime that you need on your views.
此外,如果您希望您的代码更好,请使用Eloquent 的 Date Mutators,这样您就无需每次都在视图中解析日期。
回答by Alexey Mezenin
You can parse both dates, change your code to:
您可以解析两个日期,将代码更改为:
<td class="{{ Carbon\Carbon::parse($contrat->date_facturation) < Carbon\Carbon::parse($dateNow) ? 'danger' : '' }}">
{{ \Carbon\Carbon::parse($contrat->date_facturation)->format('d/m/Y') }}
</td>
Alternatively, you can use the date mutators. In this case you'll not need to parse dates.
或者,您可以使用date mutators。在这种情况下,您不需要解析日期。
回答by Muhammad Shahzad
Compare date in blade template
比较刀片模板中的日期
I used this method of Carbon:
我使用了这种碳的方法:
@foreach($earnings as $data)
@if($data->created_at == Carbon\Carbon::today())
<tr class="alert alert-success">
@else
<tr>
@endif
....
@endforeach