Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Get difference between two dates in days using carbon

How to get the day difference between the two dates in Laravel?

In this tutorial, I will give you an example of the “How to get the day difference between the two dates in Laravel using Carbon“, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application.

Use of Carbon in Larvel

The Carbon package can help make dealing with date and time in PHP much easier and more semantic so that our code becomes more readable and maintainable. Carbon is a package by Brian Nesbit that extends PHP’s own DateTime class. It provides some nice functionality to deal with dates in PHP.

Laravel by default provides the Carbon class. Carbon class calculates the difference between two dates in days.

In this example we have two dates, we need to find the difference between these two for further calculations, I will tell you the best way to do it in Laravel Application.

resources\views\view.blade.php

We need to get the difference between two dates in the count to days using carbon in Laravel. we can simply use it to diffInDays() method in Laravel.

<div class="col-md-4">
   @php $postDate = $viewdata->created_at->format('Y-m-d H:s:i') @endphp
   @php $currentDate = date('Y-m-d H:s:i'); @endphp
   @php $date = (\Carbon\Carbon::now()->diff(\Carbon\Carbon::parse($viewadd->created_at))); @endphp
   @php $numberofDays =  $date->format('%a'); @endphp
   <div class="form-group">
      <label>{{ __('No of Days') }}</label>
      <input type="text" class="form-control" value="{{ $numberofDays }}">
   </div>
</div>

Created at Date From Database

@php $postDate = $viewadd->created_at->format('Y-m-d H:s:i') @endphp
day difference between the two dates in Laravel

Today (Current Date)

@php $currentDate = date('Y-m-d H:s:i'); @endphp
current date in laravel
current today date inLaravel

Days Difference

@php $date = (\Carbon\Carbon::now()->diff(\Carbon\Carbon::parse($viewadd->created_at))); @endphp
How to get the day difference between the two dates in Laravel?
@php $numberofDays =  $date->format('%a'); @endphp

Day difference between the two dates in Blade File in Laravel

How to get the day difference between the two dates in Laravel?
<input type="text" class="form-control" value="{{ $numberofDays }}">

In this article, we learned “How to Get Difference between two dates in days using carbon in Laravel”, I hope this article will help you with your Laravel application Project.

Read Also: Insert null data if the request value is empty in Laravel.