How to Get Difference Between Two Dates in Laravel?

By Hardik Savani November 5, 2023 Category : Laravel

Hey Developer,

This simple article demonstrates of how to get difference between two dates in laravel. It's a simple example of how to get difference between two dates in laravel carbon. you'll learn laravel get difference between two dates. This tutorial will give you a simple example of get difference between two dates carbon laravel.

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 versions.

We will use diffInDays(), diffInMonths() and diffInYears() Carbon functions to get difference between two days in days, months and years with laravel app. let's see the example code:

Example:

<?php

namespace App\Http\Controllers;

use Carbon\Carbon;

use Illuminate\Http\Request;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$toDate = Carbon::parse("2021-08-10");

$fromDate = Carbon::parse("2022-08-20");

$days = $toDate->diffInDays($fromDate);

$months = $toDate->diffInMonths($fromDate);

$years = $toDate->diffInYears($fromDate);

print_r("In Days: ". $days);

print_r("In Months: ". $months);

print_r("In Years: ". $years);

}

}

Output:

In Days: 375

In Months: 12

In Years: 1

I hope it can help you...

Tags :
Shares