I have a web page that lists events, it is split into 2 categories:
I want to use carbon to capture these dates but can't seem to get 'next week' working.
I am using query scopes for this.
So for this week:
function scopeThisWeek($query)
{
$date = Carbon::parse('this sunday')->toDateString();
return $query->where('start_date', '<', $date);
}
So this gets the date for the nearest sunday (this sunday) and then pulls any events with a start date before this sunday.
Next week:
function scopeNextWeek($query)
{
$sdate = Carbon::parse('this monday')->toDateString();
$edate = Carbon::parse('next sunday')->toDateString();
return $query->where('start_date', '>', $sdate, 'and')->where('start_date', '<', $edate);
}
Next week starts on monday so i have used 'this monday' to get that date and the query will pull any events with a greater start_date.. however i only want it to pull dates for that week so i have used 'next sunday' and added a second where clause that only pulls events less than that date... why isn't it working?
Finally is upcoming, this should pull in ANY events AFTER any of the above, so this would be 'next monday' ?
function scopeUpcoming($query)
{
$date = Carbon::parse('next monday')->toDateString();
return $query->where('start_date', '>', $date);
}
This creates the date for next monday and pulls in any events with a greater date, the behaviour of this doesn't seem right either.
Where have i gone wrong?
edit: I tested the code to see what dates it spills out and it looks like this and next are the same thing?
This sunday: 2015-02-22
This monday: 2015-02-23
Next sunday: 2015-02-22
Next monday: 2015-02-23
function scopeUpcoming($query)
{
$date = Carbon::now()->addDays(7);
return $query->where('start_date', '>', $date);
}
maybe this?
Kryptonit3 said:
function scopeUpcoming($query) { $date = Carbon::now()->addDays(7); return $query->where('start_date', '>', $date); }
maybe this?
That is a possibility but that would do from whatever the day is, so a week would go from mon to mon, tues to tues, wed to wed, etc..
I think i've nailed it:
This sunday: 2015-02-22
This monday: 2015-02-23
Next sunday: 2015-03-01
Next monday: 2015-03-02
This sunday: {{ Carbon::parse('this sunday')->toDateString() }}
This monday: {{ Carbon::parse('this monday')->toDateString() }}
Next sunday: {{ Carbon::parse('next sunday')->addDays(7)->toDateString() }}
Next monday: {{ Carbon::parse('next week')->addDays(7)->toDateString() }}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community