Jeú Casulo
Jeú Casulo

Reputation: 147

Get the difference between two dates in every rows

I need to get the difference between 2 dates but not only between 2 specific dates, I am talking about a table that has the "pdt_expires" column, and I want to show how much days until this date, but in EVERY rows. Ex:

I saw THOUSAND of solutions for only 2 specific dates but none for this case. I wonder I would need:

OR - a function inside the View that get the $product->pdt_expire and get the final value

So any help would be great, thank you.

Upvotes: 0

Views: 2650

Answers (1)

Lionel Chan
Lionel Chan

Reputation: 8059

How about achieve it using just simple DATEDIFF query:

ProductModel::select([
    'products.*',
    \DB::raw('DATEDIFF(pdt_expires, NOW()) as expires_in')
])->get();

So in every product you have, it will have an additional column called expires_in that stores different of days between NOW to pdt_expires.

DATEDIFF: How to get the number of days of difference between two dates on mysql?

Upvotes: 4

Related Questions