Laravel 9 Compare Two Carbon Dates

Laravel 9 compare two date example; Through this tutorial, i am going to show you how to compare two dates in laravel 9 using carbon date.

Laravel 9 Compare Two Carbon Dates

Now, you will see the functions to check which date is bigger, smaller or equals from two dates in laravel app, so let’s see one by one example:

  1. Laravel Carbon eq() equals
  2. Laravel Carbon ne() not equals
  3. Laravel Carbon gt() greater than
  4. Laravel Carbon gte() greater than or equals
  5. Laravel Carbon lt() less than
  6. Laravel Carbon lte() less than or equals

1 – Laravel Carbon eq() equals

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

2 – Laravel Carbon ne() not equals

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

3 – Laravel Carbon gt() greater than

<?php
  
namespace App\Http\Controllers;
  
use Carbon\Carbon;
  
class DatesController 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 – Laravel Carbon gte() greater than or equals

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

5 – Laravel Carbon lt() less than

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

6 – Laravel Carbon lte() less than or equals

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

Recommended Laravel Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.