Laravel和Carbon-DiffInDays If语句 [英] Laravel and Carbon - DiffInDays If Statement

查看:159
本文介绍了Laravel和Carbon-DiffInDays If语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我有一个相对简单的问题,我只是误解了它的一个方面.

I think I have a relatively simple question, I just think I'm misunderstanding an aspect of it.

我有一个索引页,在其中一个表单元格中有一个if语句:

I have an index page where in one of the table cells I have an if statement:

 @if (Carbon\Carbon::parse($shipment->due_date)->diffInDays(false) > 0 && Carbon\Carbon::parse($shipment->due_date)->diffInDays(false) < 10)

我要声明的地方是假,如果那一天是将来的一天,比如说明天与今天相比,我将返回-1,而如果我指的是昨天,那么我将返回1.返回.

Where the falses are is where I would like to declare that if the day is in the future, say like tomorrow compared to today, I will have a -1 returned, whereas if I refer to yesterday, I will have a 1 returned.

问题是我正在尝试使用这些文档,但是无论我尝试哪种方式,它们都在为我排队:

The problem is I am trying to use the docs but they are lining up for me no matter what sort of way I try them: http://carbon.nesbot.com/docs/#api-humandiff

但是我应该提到在相同的布局上我可以做到这一点:

However I should mention that on the same layout I can do this:

{{Carbon\Carbon::parse($shipment->due_date)->diffInDays()}}

并返回过去或将来的天数(即使两者都是正数),所以我知道上述工作原理(以某种方式,但是我仍然需要提及正数或负数).

and return the number of days in the past or future (even though both are positive) so I know the above works (in a way, but I still need the positive or negative mentioned).

推荐答案

您需要提供一个Carbon日期作为 diffInDays()的第一个参数.因此,逻辑将是:

You need to provide a Carbon date as the first parameter for diffInDays(). So, the logic will be:

Carbon\Carbon::parse($shipment->due_date)->diffInDays(now(), false)

或者:

now()->diffInDays(Carbon\Carbon::parse($shipment->due_date), false)

取决于您要实现的目标.

Depending on what exactly you're trying to achieve.

false 作为第二个参数使该方法返回带符号的值(正数或负数).

false as the second parameter makes the method return signed value (positive or negative).

这篇关于Laravel和Carbon-DiffInDays If语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆