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

FareedR's avatar

Date in Carbon

how can i make a query where current month and year onwards for the next 4 years . previous month and year will not display. example given ;

//database
id| month | 
1   2019-08-1 

//example
September 2019 - onwards
0 likes
4 replies
bobbybouwmann's avatar

You can do something like this

Model::whereDate('month', '>', Carbon::create(2019, 09, 01))->get();

If you want between now and the next 4 years you can do this

$first = Carbon::create(2019, 09, 01);
$second = $first->copy()->addYears(4);

Model::whereBetween('month', [$first, $second])->get();

Is this what you're looking for?

Sinnbeck's avatar

Something like

$now = Carbow::now();
Model::whereBetween('month', [$now, $now->copy->addYears(4)])->get();
richard's avatar

Your question is not clear... but I presume you want something like...

Model::whereMonth('created_at', '>=', now()->format('m'))
  ->whereYear('created_at', '>=', now()->format('Y'))
  ->whereDate('created_at', '<=', now()->addYears(4))
  ->get();
1 like
FareedR's avatar

@bobbybouwmann @resin @richard for more specific on my use case

if in database are empty, the system will display on selection dropdown from January 2019 until December 2023 ( if i want 4 years onwards)

if in database already have August 2019 , in selection dropdown, will not display it. 

sorry if my question confusing you guys.

Please or to participate in this conversation.