Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nionta's avatar

Get difference between two date type column in a Query

I want to find return a query like

    return $query->whereRaw('DATEDIFF(day,infos.date_of_enrollment, date("Y-    m-d"))','>',$svc_length_in_days);

exactly what should be the query in eloquent?

0 likes
4 replies
burlresearch's avatar

Here is an example of an Eloquent version of what you might be looking for.

It's not clear to me what type of database you're querying, it doesn't look like MySQL syntax, but it also doesn't look like that SQLServer sytax either (but I'm not that familiar with SQLServer...).

No matter, here's an example that works for MySQL, perhaps this will help you:

public function recent() {
    return \DB::table('posts')
        ->selectRaw('*, datediff(created_at, now())')
        ->whereRaw('datediff(created_at, now()) > ?', [-99])
        ->get();
}
1 like
jlrdw's avatar

@BURLRESEARCH - DATEDIFF is mysql. Just fyi. But I'm sure you already looked it up.

@nionta I see you are new, some of Jeffrey's lessons would help you.

Where did you come up with

$query->whereRaw('DATEDIFF(day,infos.date_of_enrollment, date("Y-    m-d"))','>',$svc_length_in_days);

@BURLRESEARCH I can assure you that is not sql server. Oh well.

Please or to participate in this conversation.