Laravel Carbon 查看日期是否在过去
Laravel Carbon See if date is in the past
我对此很困惑,也许是我没有看到的简单问题。如果我想查看某个日期是否在今天的过去,我应该可以这样做吗?
if( $league->date_start <= Carbon::now() ){
$join = false;
$message = 'Sorry, the league has already started';
}
如果我把日期转储出来
$league->date_start = 2017-07-31 00:00:00
Carbon::now() = 2017-11-01 16:29:27
$league->date_start
是一个受保护的日期,因此它是一个碳实例
但这行不通,如果我将其切换为 $league->date_start >= Carbon::now()
它会起作用并且不会让我加入。我知道 "league" 开始日期是过去的所以不应该是 $league->date_start <= Carbon::now()
?????
检查 carbon docs 上的“比较”部分。您应该调用 $first->lte($second)
来比较两个碳实例。
尝试使用 if ($league->date_start->diffInSeconds() >= 0)
。该方法 diffInSeconds
returns 当前时间与您的碳实例之间的差异。
这是 tinker 的输出示例 -
>>> $now = \Carbon\Carbon::now();
=> Carbon\Carbon {#648
+"date": "2017-11-01 16:41:04.000000",
+"timezone_type": 3,
+"timezone": "UTC",
}
>>> $now->diffInSeconds();
=> 5
>>> $now->diffInSeconds();
=> 7
>>> $now->diffInSeconds();
=> 8
>>> $now->diffInSeconds();
=> 10
>>> $now->diffInSeconds() > 0
=> true
有内置的 Carbon 方法 isPast
所以你可以使用:
$league->date_start->isPast()
判断日期是否过去
要检查 $date
是否位于(大于或等于今天的一天)或(今天的前一天),请使用:
function isPast($date){
return Carbon::now()->startOfDay()->gte($date);
}
// true : $date is in past
// false: $date is in today or future
将日期与 Carbon 进行比较的基本思想是,两个日期都必须采用 Carbon 格式,在您的情况下 Carbon::now()
是但 $league->date_start
不是,可以通过使用Carbon::parse()
有很多方法可以实现这一点:
- 通过使用
<=
:
if(Carbon::parse($league->date_start) <= Carbon::now()){
$join = false;
$message = 'Sorry, the league has already started';
}
- 通过使用
lte
:
if(Carbon::parse($league->date_start)-> lte(Carbon::now()){
$join = false;
$message = 'Sorry, the league has already started';
}
- 通过使用
lessThanOrEqualTo
:
if(Carbon::parse($league->date_start)->lessThanOrEqualTo(Carbon::now()){
$join = false;
$message = 'Sorry, the league has already started';
}
我对此很困惑,也许是我没有看到的简单问题。如果我想查看某个日期是否在今天的过去,我应该可以这样做吗?
if( $league->date_start <= Carbon::now() ){
$join = false;
$message = 'Sorry, the league has already started';
}
如果我把日期转储出来
$league->date_start = 2017-07-31 00:00:00
Carbon::now() = 2017-11-01 16:29:27
$league->date_start
是一个受保护的日期,因此它是一个碳实例
但这行不通,如果我将其切换为 $league->date_start >= Carbon::now()
它会起作用并且不会让我加入。我知道 "league" 开始日期是过去的所以不应该是 $league->date_start <= Carbon::now()
?????
检查 carbon docs 上的“比较”部分。您应该调用 $first->lte($second)
来比较两个碳实例。
尝试使用 if ($league->date_start->diffInSeconds() >= 0)
。该方法 diffInSeconds
returns 当前时间与您的碳实例之间的差异。
这是 tinker 的输出示例 -
>>> $now = \Carbon\Carbon::now();
=> Carbon\Carbon {#648
+"date": "2017-11-01 16:41:04.000000",
+"timezone_type": 3,
+"timezone": "UTC",
}
>>> $now->diffInSeconds();
=> 5
>>> $now->diffInSeconds();
=> 7
>>> $now->diffInSeconds();
=> 8
>>> $now->diffInSeconds();
=> 10
>>> $now->diffInSeconds() > 0
=> true
有内置的 Carbon 方法 isPast
所以你可以使用:
$league->date_start->isPast()
判断日期是否过去
要检查 $date
是否位于(大于或等于今天的一天)或(今天的前一天),请使用:
function isPast($date){
return Carbon::now()->startOfDay()->gte($date);
}
// true : $date is in past
// false: $date is in today or future
将日期与 Carbon 进行比较的基本思想是,两个日期都必须采用 Carbon 格式,在您的情况下 Carbon::now()
是但 $league->date_start
不是,可以通过使用Carbon::parse()
有很多方法可以实现这一点:
- 通过使用
<=
:
if(Carbon::parse($league->date_start) <= Carbon::now()){
$join = false;
$message = 'Sorry, the league has already started';
}
- 通过使用
lte
:
if(Carbon::parse($league->date_start)-> lte(Carbon::now()){
$join = false;
$message = 'Sorry, the league has already started';
}
- 通过使用
lessThanOrEqualTo
:
if(Carbon::parse($league->date_start)->lessThanOrEqualTo(Carbon::now()){
$join = false;
$message = 'Sorry, the league has already started';
}