Laravel Carbon See if date is in the past
Asked Answered
M

5

48

I am very confused by this, maybe its something simple I am not seeing. If I want to see if a date is in the past of today I should be able to do something like this?

if( $league->date_start <= Carbon::now() ){
    $join  = false;
    $message = 'Sorry, the league has already started';
}

if I dump out the dates its

$league->date_start = 2017-07-31 00:00:00
Carbon::now() = 2017-11-01 16:29:27

$league->date_start is a protected date so its a carbon instance

But this doesnt work, if I switch it to $league->date_start >= Carbon::now() it works and wont let me join. I know the "league" start date is in the past so shouldnt it be $league->date_start <= Carbon::now()?????

Mosemoseley answered 1/11, 2017 at 16:31 Comment(0)
F
132

There's built-in Carbon method isPast so you can use:

$league->date_start->isPast()

to determine if date is in past or not

Fehr answered 1/11, 2017 at 16:42 Comment(2)
Source: github.com/briannesbitt/Carbon/blob/2.28.0/src/Carbon/Traits/…Hagbut
The question asks if it is past from today, not if it is in the past in general.Iva
I
7

To check wether if $date lies in a (day greater or equal than today) OR (day before today) use :

 function isPast($date){ 

    return  Carbon::now()->startOfDay()->gte($date);

 }


  // true :  $date is in past

  // false:  $date is in today or future
Iva answered 7/10, 2021 at 10:51 Comment(0)
I
5

Check the section “Comparison” on carbon docs. You should call $first->lte($second) to compare two carbon instances.

Impressive answered 1/11, 2017 at 16:37 Comment(1)
Thanks. This worked. Technically mine worked too, there was just a bad chuck of code that was blocking itMosemoseley
S
2

Try using if ($league->date_start->diffInSeconds() >= 0). The method diffInSeconds returns the difference between the current time and your carbon instance.

Here's an example of an output from 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
Socinus answered 1/11, 2017 at 16:39 Comment(0)
M
2

The Basic idea behind comparing dates with Carbon is that both the dates must has to be in Carbon format, in your case Carbon::now() is but $league->date_start is not, which can be converted by using Carbon::parse()

There are many ways by which this can be achieved :

  • By using <= :
if(Carbon::parse($league->date_start) <= Carbon::now()){
    $join  = false;
    $message = 'Sorry, the league has already started';
}
  • by using lte :
if(Carbon::parse($league->date_start)-> lte(Carbon::now()){
    $join  = false;
    $message = 'Sorry, the league has already started';
}
  • by using lessThanOrEqualTo :
if(Carbon::parse($league->date_start)->lessThanOrEqualTo(Carbon::now()){
    $join  = false;
    $message = 'Sorry, the league has already started';
}
Mackler answered 5/1, 2022 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.