9 months ago

Carbon - Get Date Depending on now() value

Posted 9 months ago by nickdavies07

I'm creating a scope in my model for retrieving values from a database between two dates.

So far, I have this which retrieves entries between 1st Sept 2018 and now. However, I need the first date's year to vary depending on the current date.

I basically need to go back from today to the 1st September 20{xx} depending on the current year, so if it's 26th November 2018, I need to get 1st September 2018, until now() gets passed 1st September 2019, then it should get values between 1st September 2019 and e.g. 7th September 2019.

This is what I have so far, I'm just not sure on how to use Carbon to get the correct date I'm looking for.

    public function scopeResolvedThisAcademicYear($query)
        return $query->whereBetween('resolved_on', ['2018-09-01', Carbon::now()]);

Any help would be hugely appreciated!

Please sign in or create an account to participate in this conversation.