skip to Main Content

I’am very confused to get someone’s birthday date by changing Y-m-d to m-d in the date_birth field. How should I do? This is my current code:

$getBirthday = Karyawan::where('date_birth', Carbon::now()->format('m-d'))->get();

Thanks, I want to get someone’s birthday by taking m-d only from date_birth field. Hope u’re helping me!

2

Answers


  1. You can query using the mysql DATE_FORMAT method and using whereRaw

    $getBirthday = Karyawan::whereRaw("DATE_FORMAT(birthdate, '%m-%d') = ?", [Carbon::now()->format('m-d')])->get();
    
    Login or Signup to reply.
  2. If you want to get in the day and month same, you can also use this:

    $getBirthday = Karyawan::whereDay('date_birth', Carbon::now()->format('d'))
                       ->whereMonth('date_birth', Carbon::now()->format('m'))->get();
    

    See also: https://laravel.com/docs/9.x/queries#additional-where-clauses

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search