Solved

How to SELECT the number of days that have elapsed in laravel query builder select statement

Posted on 2016-09-22
2
77 Views
Last Modified: 2016-09-26
Hi,
I'm trying to put together a query that as part of the select, it takes the datetime stamp that the row was added  and todays date and returns the number of days that have passed.


Guessing something along the lines of below (just roughing it out to help demonstrate the problem):
DB::raw('(now() - used_vehicles.added) AS num_of_days')

My query so far without the addition (I took out a few additional columns to keep it shorter)
$used_vehicles = DB::table('used_vehicles')
		->select(['used_vehicles.id','used_vehicles.construction_year','used_vehicles.stock_number','used_vehicles.vehicle_price','used_vehicles.discount',DB::raw('(used_vehicles.vehicle_price-used_vehicles.discount) AS selling_price')])
        ->join('dealerships', 'used_vehicles.dealer_id', '=', 'dealerships.id')
        ->join('makes', 'used_vehicles.make_id', '=', 'makes.id')
        ->join('models', 'used_vehicles.model_id', '=', 'models.id')
        ->join('used_vehicles_categories', 'used_vehicles.category_id', '=', 'used_vehicles_categories.id')
        ->where('used_vehicles.added','<',Carbon::now()->subDays($aged))
        ->where($whereClause);
		
		return Datatables::of($used_vehicles)

Open in new window


Any idea how to add the days passed into my select?
0
Comment
Question by:tjyoung
2 Comments
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 41811544
Try DATEDIFF


DATEDIFF(used_vehicles.added, CURRDATE())

DB::raw('DATEDIFF(used_vehicles.added, CURRDATE()) AS num_of_days')
0
 
LVL 1

Author Comment

by:tjyoung
ID: 41817189
thats awesome. Slight change to code (typo) CURDATE()
Otherwise, snapped right in.
Thanks again
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now