1

I want to show the value of first_name and birth_date of customers who were birthday today. What kind of queries should I do?

  $birthday = DB::table('customers')
  ->select(DB::raw('customers.first_name','customers.birth_date'))
  ->whereRaw('DAYOFYEAR(curdate()) <= DAYOFYEAR(birth_date) AND DAYOFYEAR(curdate()) + 0 >=  dayofyear(birth_date)')
  ->orderByRaw('DAYOFYEAR(birth_date)')
  ->get();

2 Answers2

0

You can use this:

 $birthday = DB::table('customers')
  ->select(DB::raw('customers.first_name','customers.birth_date'))
  ->whereRaw("DATE_FORMAT(FROM_UNIXTIME(birth_date),'%m-%d') = DATE_FORMAT(NOW(),'%m-%d')")
  ->orderByRaw('DAYOFYEAR(birth_date)')
  ->get();

Reference: Use MySQL to determine whether today is a user's birthday

Sehdev
  • 5,486
  • 3
  • 11
  • 34
0

You can get the results by using the following query...

$today = \Carbon\Carbon::today()->toDateString();

$birthday = DB::table('customers')
    ->select('customers.first_name','customers.birth_date')
    ->whereDate('birth_date', $today)
    ->get();
Jasmel Singh
  • 111
  • 1
  • 6