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
Solved

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

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 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