MariaJen
MariaJen

Reputation: 1143

how to compare two date in laravel

I'm having a hard time in comparing two date in laravel, I try this code below in mysql and it works well but when I transfer it on laravel it doesn't work

SELECT * FROM `holiday` WHERE date_format(holiday.date,'%m-%d') = date_format('2017-05-15','%m-%d')

here is my code in laravel

public function getHoliday(){
 $date = '2017-05-15';

         $data = DB::table('holiday')
         ->select(
            'holiday.id'
            )
        ->whereRaw("date_format(holiday.date, '%m-%d') "=" date_format($date, '%m-%d')")

        ->first();



        return $data;

    }

I do hope you could help me with my code, I just want to compare day and month of two date

Upvotes: 1

Views: 2447

Answers (2)

Mihai Matei
Mihai Matei

Reputation: 24276

$data = DB::table('holiday')
    ->select([
        'holiday.id',
        'holiday.date',
    ])
    ->whereRaw("date_format(holiday.date, '%m-%d') = date_format('$date', '%m-%d')")
    ->first();

Upvotes: 1

Onix
Onix

Reputation: 2179

Try changing this line :

->whereRaw("date_format(holiday.date, '%m-%d') "=" date_format($date, '%m-%d')")

to this

 ->where(date_format(holiday.date, '%m-%d') , date_format($date, '%m-%d'))

EDIT.

try to use this date helpers instead

          ->whereDay('date', '=', date('d'))
          ->whereMonth('date', '=', date('m'))
          ->first();

Upvotes: 1

Related Questions