Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Carbon See if date is in the past

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()?????

like image 874
Packy Avatar asked Nov 01 '17 16:11

Packy


People also ask

How do you find the date on a Carbon object?

You can use array_map to produce this kind of result: $dates_formatted = array_map(function($entry) { // transform the Carbon object to something like 'Dec 25, 1975' return $entry['date']->toFormattedDateString(); }, $dates);

What is Carbon :: now ()?

Carbon::now returns the current date and time and Carbon:today returns the current date. $ php today.php 2022-07-13 15:53:45 2022-07-13 00:00:00. This is a sample output. Carbon::yesterday creates a Carbon instance for yesterday and Carbon::tomorrow for tomorrow.


1 Answers

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

$league->date_start->isPast() 

to determine if date is in past or not

like image 148
Marcin Nabiałek Avatar answered Sep 27 '22 15:09

Marcin Nabiałek