skip to Main Content

I Have following query for mysql.

phpMyAdmin 4.0.10.10

SELECT * FROM `table_name` WHERE DATEDIFF(NOW(), start_date) = 154

I need an equivalent query for Laravel to use DATEDIFF function in where clause.

2

Answers


  1. As in you comment, what you need for laravel,

    $oldDate = Carbon::now()->subDays(154));
    Table::whereDate('start_date', '=', $oldDate); 
    

    Another way is:

    DB::table('table_name')->whereRaw('DATEDIFF(current_date, start_date) = 154')->get();
    
    Login or Signup to reply.
  2. You can try with something like this:

     SELECT * FROM `table_name` WHERE start_date = (NOW() - 154 days);
    

    I will not recommend DATEDIFF in the WHERE cuz it could possibly impose a table scan. Instead, I suggest this:

     SELECT
     DATEDIFF(start_date, NOW()) AS daysDiffs
     FROM table_name
     WHERE start_date = DATE(NOW() - INTERVAL 154 DAYS);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search