Carbon create date range June 1st - May 31st
Hi,
I would like to create a date range in my controller between June 1st and May 31 and create queries with this date range? Can anyone help with this?
Thanks in advance
do you mean, you need validation for the date field to this date range?
Sorry i didn't give much detail.
To give some context, my app is for farming and the farming season starts 1st june and finishes 31st may the following year.
I would like to make queries on the database for records for the current farming season.
Does that help?
@matt_panton thanks for the link. Any ideas how I would do that without hardcoding the year?
@sanch012 Here is easy way to do it
$year = $request->input('year', '2018');
$startDate = Carbon::createFromFormat('Y-m-d', $year . '-06-01');
$endDate = $startDate->copy()
->addYear()
->modify('-1 day');
User::whereDate('created_at', '>=', $startDate)
->whereDate('created_at', '<=', $endDate)
->get();
//or
User::whereBetween('created_at', [
$startDate->format('Y-m-d'),
$endDate->format('Y-m-d'),
])->get();
$startDate = Carbon::create(null,5,27);
$endDate = Carbon::create(null,5,29);
User::whereBetween( 'created_at', [ $startDate, $endDate ] )->get();
null will get the current year while creating.
Please or to participate in this conversation.