Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sanch012's avatar

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

0 likes
6 replies
BishoyWagih's avatar

do you mean, you need validation for the date field to this date range?

sanch012's avatar

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?

Yorki's avatar

@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();
himanshu-dhiman's avatar
$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.

2 likes

Please or to participate in this conversation.