Laravel

Laravel 8 Compare Two Carbon Dates

Pinterest LinkedIn Tumblr

How to Compare Two Dates in Laravel Carbon?

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

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);
    }
}

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);
    }
}

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);
    }
}

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);
    }
}

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);
    }
}

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);
    }
}
Faster PHP Cloud Hosting