Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aac2018's avatar

How to get date difference for date column against current date in Laravel ?

I want to add all loan_amort values where schedule is <= 30 days when schedule is subtracted against the current date. How would I do that? Here is my sample code. I couldn't figure out the whereRaw part. Please help. Thank you.

$current = Amortization::select('loan_type', \DB::raw('SUM(loan_amortization) as total_current'))
                                ->where('payment_status',0)
                                ->whereRaw('date_diff(schedule,now()) <= 30')
                                ->groupBy('loan_type')
                                ->orderBy('loan_type')
                                ->get();
0 likes
9 replies
EslamAhmed's avatar

You can use carbon to format any date as you like https://carbon.nesbot.com/docs/

//To add 30 days from now and get the date formatted
$date = Carbon::now()->addDays(30)->toDateTimeString();

//To subtract 30 days from now and get the date formatted
$date = Carbon::now()->subDays(30)->toDateTimeString();

Then you can use this date in your query condition

$current = Amortization::select('loan_type', \DB::raw('SUM(loan_amortization) as total_current'))
                                ->where('payment_status',0)
                                ->where('schedue', '<=', $date)
                                ->groupBy('loan_type')
                                ->orderBy('loan_type')
                                ->get();
aac2018's avatar

Hi thank you for responding. I just very confused on this. What I really wanted to get is the difference of schedule - current_date that is ranging from 1-30 days.

aac2018's avatar

What if the schedule date is August 09, 2019 then the current date is August 11, 2019. Adding and subtracting 30 days to current date will produce different result. What I need is the exact difference of the schedule and the current date.

siangboon's avatar

assume $date1 = $schedule_date $date2 = now()

$differentValue = date_diff($date1,$date2) will returns the difference between two DateTime objects in this case the number of days

hence

->whereRaw('date_diff(schedule,now()) <= 30')

will give you something ->where( $differentValue <= 30);

EslamAhmed's avatar

A little bit of basic mathematics, so you can understand the idea of comparison you want to do:

If the desired result is schedule - current_date <= 30 and also schedule - current_date => 1

hence current_date < schedule <= current_date + 30

So the final code will be

$maxDate = Carbon::now()->addDays(30)->toDateTimeString();
$minDate = Carbon::now()->toDateTimeString();
$current = Amortization::select('loan_type', \DB::raw('SUM(loan_amortization) as total_current'))
                                ->where('payment_status',0)
                                ->whereDate('schedue', '<=', $maxDate) // Difference is equal or less than 30
                                ->whereDate('schedue', '>', $minDate) // Difference is greater than 1
                                ->groupBy('loan_type')
                                ->orderBy('loan_type')
                                ->get();
aac2018's avatar

I guess I am not that clear and I am very sorry for that. Even I is so confused on this. But what I really wanted is to add the loan_amort for the schedules that when subtracted from the current date will result to a difference ranging from 1-30 days. Note that the current date should be greater than the schedule . When the current date is greater than the schedule it means the schedule is already due.

aac2018's avatar

I will be persistent to you sir. There is a little bit of confusion but you are almost there. Yours is schedule - current date <= 30, I need the opposite current date - schedule <= 30 I don't know how to re-structure your code. Please help me. Thank you.

aac2018's avatar

Thank you so much for responding. I will try this when I come back in my workplace. This is seems to be good. But I forgot to tell you sir that schedule is a varchar datatype. Will this still work?

Please or to participate in this conversation.