Laravel Carbon See if date is in the past

前端 未结 3 1936
慢半拍i
慢半拍i 2020-12-17 08:34

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?

相关标签:
3条回答
  • 2020-12-17 09:17

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

    0 讨论(0)
  • 2020-12-17 09:18

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

    $league->date_start->isPast()
    

    to determine if date is in past or not

    0 讨论(0)
  • 2020-12-17 09:18

    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
    
    0 讨论(0)
提交回复
热议问题