Laravel 10 Compare Two Carbon Dates Example

If you have 2 carbon dates. And you want to get the difference by comparing these dates. then follow this tutorial. In this tutorial, you will learn how to compare two dates in Laravel 10 with carbon.

Laravel 10 Compare Two Carbon Dates Example

By using the method given below, you can compare 2 carbon dates and find the difference between them.

  • eq() equals
  • ne() not equals
  • gt() greater than
  • gte() greater than or equals
  • lt() less than
  • 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 Posts

Recommended:- Laravel Try Catch

AuthorDevendra Dode

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 *