cyberace's avatar

How can I get the results of the upcoming month's birthdays in Laravel

I have this code but I can't query only birthday of today. What should I do if I need this query type?

    public function birth(Request $request)
{
  
$today = Carbon::now()->format('Y-m-d');


  $birthday = DB::table('customers')
                     ->select('id_user','first_name','last_name','birth_date','open_user_date')
                     ->where('birth_date',$today)
                     ->get();



      return view('reports/birth',compact('birthday','date','today','hbddate'));
  }
0 likes
10 replies
cyberace's avatar

@sti3bas i want the year difference between the date of birth and today to the date of birth.

a4ashraf's avatar

Hi @cyberace

Try this

select(DB::raw('FLOOR(DATEDIFF(birth_date,Carbon::today())/365) as Diff'))

Sinnbeck's avatar

If their birthday must be today, the difference will always be 0?

Or do you just mean you want all users with an upcoming birthday for the end of the year?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Ok I think I understand

whereDay('birth_date', Carbon::now())->whereMonth('birth_date', Carbon::now())

If you need to get the number of years you can easily use an accessor

fylzero's avatar

Just as a tip... in Laravel you don't need to use Carbon::today() or Carbon::now()

You can simply use today() or now() as they are date helpers in Laravel that resolve a Carbon instance.

Also, just easier to read imo.

vendor/laravel/framework/src/Illuminate/Foundation/helpers.php

CC: @cyberace @sinnbeck @sti3bas @a4ashraf

Please or to participate in this conversation.