Solved

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

Posted on 2016-09-22
2
94 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

810 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