Solved

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

Posted on 2016-09-22
2
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 41

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

624 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