ernest
October 14, 2021, 9:04am
1
On a page displaying events I filtered the events into upcoming and past events:
controller:
$upcoming = $events->filter(function ($child) {
return $child->date()->toDate() > time();
})
$past = $events->filter(function ($child) {
return $child->date()->toDate() < time();
})
But here comes the problem: The events get moved to the “past” section before the date is over. Big Problem: Tonight’s event is already marked as “past” right now. Yesterday it was still upcoming but today it has moved to past!
What can I do? Help! (my client is going nuts right now!)
The problem is that you compare tUnix timestamps rather than day based dates. Also, consider using >=
instead of >
.
ernest
October 14, 2021, 9:44am
3
I used >= for the upcoming events and my event is still marked as past. I also tried to do a new “today” section like this
$today = $events->filter(function ($child) {
return $child->date()->toDate() == time();
})
but the event is still set as past. I even tried DateTime() instead of time() and then my site crashed.
I’m using the strftime date handler.
What else can I try?
If you want to compare day (Y-m-d) based, I would use strtotime('today')
to get start of today always:
// upcoming
return $child->date()->toDate() > strtotime('today');
// past
return $child->date()->toDate() < strtotime('today');
ernest
October 14, 2021, 10:12am
5
Yes, that did it! Thank you!