How to Compare Two Dates in Laravel Carbon?


Share:
Published By: Admin, Published On: Sep 10, 2021, Category: PHP Laravel 8 Laravel


Hi Dev,

Today, In this example, i will explain to you how to compare two dates in laravel.so it can easy to use in laravel app.

You can easily compare two dates in laravel carbon, how to compare two dates in laravel, laravel carbon compares dates example, compare two dates in laravel, compare dates carbon laravel, carbon compare dates without time.

So, many functions compare two dates with following function..

  • eq() equals
  • ne() not equals
  • gt() greater than
  • gte() greater than or equals
  • lt() less than
  • lte() less than or equals

Here, I will give you full example for how to compare two dates in laravel using carbon so follow my all steps.

Laravel Carbon eq() equals
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon compare two date..
     *
     * @return string
    */
    public function index()
    {
        $date1 = Carbon::createFromFormat('m/d/Y H:i:s', '09/10/2020 10:41:00');
        $date2 = Carbon::createFromFormat('m/d/Y H:i:s', '09/10/2020 10:41:00');
  
        $result = $date1->eq($date2);
        var_dump($result);
    }

}
Output
bool(true)
Laravel Carbon ne() not equals
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon compare two date..
     *
     * @return string
    */
    public function index()
    {
        $date1 = Carbon::createFromFormat('m/d/Y H:i:s', '09/10/2020 10:41:00');
        $date2 = Carbon::createFromFormat('m/d/Y H:i:s', '09/10/2020 20:41:00');
  
        $result = $date1->ne($date2);
        var_dump($result);
    }

}
Output
bool(true)
Laravel Carbon gt() greater than
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon compare two date..
     *
     * @return string
    */
    public function index()
    {
        $date1 = Carbon::createFromFormat('m/d/Y H:i:s', '09/10/2020 10:41:00');
        $date2 = Carbon::createFromFormat('m/d/Y H:i:s', '09/09/2020 20:41:00');
  
        $result = $date1->gt($date2);
        var_dump($result);
    }

}
Output
bool(true)
Laravel Carbon gte() greater than or equals
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon compare two date..
     *
     * @return string
    */
    public function index()
    {
        $date1 = Carbon::createFromFormat('m/d/Y H:i:s', '09/09/2020 10:41:00');
        $date2 = Carbon::createFromFormat('m/d/Y H:i:s', '09/09/2020 10:41:00');
  
        $result = $date1->gte($date2);
        var_dump($result);
    }

}
Output
bool(true)
Laravel Carbon lt() less than
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon compare two date..
     *
     * @return string
    */
    public function index()
    {
        $date1 = Carbon::createFromFormat('m/d/Y H:i:s', '09/09/2020 10:41:00');
        $date2 = Carbon::createFromFormat('m/d/Y H:i:s', '09/10/2020 10:41:00');
  
        $result = $date1->lt($date2);
        var_dump($result);
    }

}
Output
bool(true)
Laravel Carbon lte() less than or equals
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon compare two date..
     *
     * @return string
    */
    public function index()
    {
        $date1 = Carbon::createFromFormat('m/d/Y H:i:s', '09/10/2020 10:41:00');
        $date2 = Carbon::createFromFormat('m/d/Y H:i:s', '09/10/2020 10:41:00');
  
        $result = $date1->lte($date2);
        var_dump($result);
    }

}
Output
bool(true)

It Will Help You..

Share: