hupp's avatar
Level 11

How to add one year to datetime field in query ?

I want to fetch data which created_at database field plus 1 year which is greater then current time. I have tried below code but it did not work.

 WHERE DATE(DATE_ADD(created_at, INTERVAL -1 YEAR)) >= CURRENT_DATE

 UserModel::where(DB::raw("created_at + INTERVAL 1 YEAR"),'>=',date("Y-m-d H:i:s"))->orderBy('id','DESC')->get()

Could anyone please help me ?

0 likes
2 replies
tisuchi's avatar

@phphupptechnologies

Have you tried this?

$date = UserModel::whereRaw('"Y-m-d H:i:s" >= DATE_ADD(created_at, INTERVAL 1 YEAR)')
                ->orderBy('id', 'desc')
                ->get()
mohamedPhp's avatar

if the code not work try this $data = modelName::select('table.id') ->where('taches.catache_id', 1) ->whereRaw('DATE_ADD(date_fin_realisation, INTERVAL 2 DAY) <= ?', [$date]) ->get();

Please or to participate in this conversation.