Home > Back-end >  How to parsing date column to month & day only?
How to parsing date column to month & day only?

Time:11-09

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!

CodePudding user response:

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();

CodePudding user response:

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

  • Related