How to Compare Two Dates in Laravel Carbon?

How to Compare Two Dates in Laravel Carbon?

To compare two or more dates and calculate the difference between them in the number of days, months, years, hours, seconds, etc, you can do this using Carbon in Laravel.

Carbon is a PHP library that helps you work with dates and times, it has a lot of functions; Here are some carbon functions for date and time:

  • eq(): Equals
  • ne(): Not equals
  • gt(): Greater than
  • gte(): Greater than or equals
  • lt(): Less than
  • lte(): Less than or equals

How to Compare Two Dates in Laravel Carbon?

Here are some examples to compare carbon two or more dates and calculate the difference between them in the number of days, months, years, hours, seconds, etc in Laravel:

1 – Compare Two Dates with Time using eq()

The carbon eq() function checks if two values are equal to each other with times in laravel; Here is an example:

<?php
  
namespace App\Http\Controllers;
  
use Carbon\Carbon;
  
class CarDatesController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $date1 = Carbon::createFromFormat('m/d/Y H:i:s', '10/02/2021 11:30:00');
        $date2 = Carbon::createFromFormat('m/d/Y H:i:s', '10/02/2021 11:30:00');
  
        $result = $date1->eq($date2);
        var_dump($result);
    }
}

The output of the code will be true or false.

2 – Compare two dates not equal in laravel

The carbon not() function checks if two values are not equal to each other with times in Laravel; Here is an example:

<?php
  
namespace App\Http\Controllers;
  
use Carbon\Carbon;
  
class CarDatesController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $date1 = Carbon::createFromFormat('m/d/Y H:i:s', '10/01/2021 12:38:00');
        $date2 = Carbon::createFromFormat('m/d/Y H:i:s', '12/01/2021 12:38:00');
  
        $result = $date1->ne($date2);
        var_dump($result);
    }
}

The output of the code will be true or false.

3 – Laravel where datetime greater than

The carbon gt() function checks one date time is greater than date time in laravel; Here is an example:

<?php
  
namespace App\Http\Controllers;
  
use Carbon\Carbon;
  
class CarDatesController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $date1 = Carbon::createFromFormat('m/d/Y H:i:s', '12/01/2020 11:20:00');
        $date2 = Carbon::createFromFormat('m/d/Y H:i:s', '12/01/2020 10:20:00');
  
        $result = $date1->gt($date2);
        var_dump($result);
    }
}

4 – Carbon dates greater than or equals

The carbon gte() function checks if one value is greater than or equal to another; Here is an example:

<?php
  
namespace App\Http\Controllers;
  
use Carbon\Carbon;
  
class CarDatesController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $date1 = Carbon::createFromFormat('m/d/Y H:i:s', '10/05/2022 12:40:00');
        $date2 = Carbon::createFromFormat('m/d/Y H:i:s', '10/05/2022 12:40:00');
  
        $result = $date1->gte($date2);
        var_dump($result);
    }
}

5 – Carbon lt() less than

The carbon lt() checks if one date time is less than another, here is an example:

<?php
  
namespace App\Http\Controllers;
  
use Carbon\Carbon;
  
class CarDatesController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $date1 = Carbon::createFromFormat('m/d/Y H:i:s', '11/05/2021 09:20:00');
        $date2 = Carbon::createFromFormat('m/d/Y H:i:s', '11/05/2021 10:20:00');
  
        $result = $date1->lt($date2);
        var_dump($result);
    }
}

6 – Carbon lte() less than or equals

The carbon lte() checks if one dete time is less than or equal to date time; here is an example:

<?php
 
namespace App\Http\Controllers;
  
use Carbon\Carbon;
  
class CarDatesController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $date1 = Carbon::createFromFormat('m/d/Y H:i:s', '09/04/2020 09:55:00');
        $date2 = Carbon::createFromFormat('m/d/Y H:i:s', '09/04/2020 09:55:00');
  
        $result = $date1->lte($date2);
        var_dump($result);
    }
}

7 – Carbon difference between two dates in Years, Months, and Days

You can use diffInDays, diffInMonths,and diffInYears functions to compare dates and get the difference between year, month, and day.

Here is an example to calculate the difference between two carbon dates in years, months, and days:

<?php
  
namespace App\Http\Controllers; 
use Carbon\Carbon;
   
class CarDatesController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $date1 = Carbon::now();
        $date2 = Carbon::parse("2026-05-10");
  
        $getDiffInDays = $date1->diffInDays($date2);
        $getDiffInMonths = $date1->diffInMonths($date2);
        $getDiffInYears = $date1->diffInYears($date2);

        dd($getDiffInDays,$getDiffInMonths,$getDiffInYears);
    }
}

8 – Carbon difference between two dates in Hours, Minutes and Seconds

You can use diffInHours, diffInMinutes,and diffInSeconds functions to compare dates and get the difference between in hours, minutes, and seconds.

Here is an example to calculate the difference between two carbon dates in hours, minutes, and seconds:

<?php
  
namespace App\Http\Controllers; 
use Carbon\Carbon;
   
class CarDatesController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
      $date1 = Carbon::createFromFormat("Y-m-d H:i:s", "2025-05-04 00:00:00");
      $date2 = Carbon::createFromFormat("Y-m-d H:i:s", date("Y-m-d H:i:00"));

  
      $getDiffInHours = $date1->diffInHours($date2);
      $getDiffInMinutes = $date1->diffInMinutes($date2);
      $getDiffInSeconds = $date1->diffInSeconds($date2);

        dd($getDiffInHours,$getDiffInMinutes,$getDiffInSeconds);
    }
}

Conclusion

You have learned how to compare two or more carbon dates in different ways in laravel apps.

Recommended Laravel Posts

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *