Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-09-22
2
Medium Priority
?
191 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 2000 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 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