Search

How to compare two date in laravel ?

  • Share this:
post-title

Welcome guys, In this article Here, i will show you the laravel carbon class check if date is greater than other date. if you have question about laravel carbon compare two dates then i will give simple example with solution. you can easily check if date is bigger than today date using carbon in laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 version.

What is the carbon class laravel ? 

Carbon is a package by Brian Nesbit that extends PHP's own DateTime class. It provides some nice functionality to deal with dates in PHP. Specifically things like: Dealing with timezones. Getting current time easily.

It provides some nice functionality to deal with dates in PHP. Specifically things like:

  1. Dealing with timezones.
  2. Getting current time easily.
  3. Converting a datetime into something readable.
  4. Parse an English phrase into datetime ("first day of January 2016").
  5. Add and Subtract dates ("+ 2 weeks", "-6 months").
  6. Semantic way of dealing with dates.

Setting Up the Project 


In order to use Carbon, you’ll need to import Carbon from the Carbon namespace. Luckily for us, Carbon is already included in Laravel. Whenever we need to use Carbon, we can import it like so:

<?php
use Carbon\Carbon;

Getting a Specific Date and Time

Get the current time:

$currentDateTime = Carbon::now();

Current time can also be retrieved with this instantiation:

$current = new Carbon();

Get today’s date:

$todayDateTime = Carbon::today();

Get yesterday’s date:

$yesterdayDateTime = Carbon::yesterday();

Get tomorrow’s date:

$tomorrowDateTime = Carbon::tomorrow();

Parse a specific string:

$newYear = new Carbon('first day of January 2016');

Step 1: Add days to a specific date time

<?php
  
namespace App\Http\Controllers; 
use Carbon\Carbon;
   
class CarbonExampleController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $currentDateTime = Carbon::now()->addDays(10);
        dd($currentDateTime);
    }
}

Step 2: Add Minutes to a specific date time

<?php
  
namespace App\Http\Controllers; 
use Carbon\Carbon;
   
class CarbonExampleController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $currentDateTime = Carbon::now()->addMinutes(10);
        dd($currentDateTime);
    }
}

Step 3: Check greater date Or time

<?php
  
namespace App\Http\Controllers; 
use Carbon\Carbon;
   
class CarbonExampleController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $dateTime = Carbon::now()->addMinutes(10);
	$currentDateTime = Carbon::now();

	$checkDateTime = $currentDateTime->gt($currentDateTime)
        dd($checkDateTime);
    }
}

Step 4: Check less than date Or time


<?php
  
namespace App\Http\Controllers; 
use Carbon\Carbon;
   
class CarbonExampleController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $dateTime = Carbon::now()->addMinutes(10);
	$currentDateTime = Carbon::now();

	$checkDateTime = $currentDateTime->lt($currentDateTime)
        dd($checkDateTime);
    }
}

Now back to your actual question. Carbon has a bunch of comparison functions:

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

Carbon has a bunch of comparison functions with mnemonic names:

  1. equalTo() equals
  2. notEqualTo() not equals
  3. greaterThan() greater than
  4. greaterThanOrEqualTo() greater than or equals
  5. lessThan() less than
  6. lessThanOrEqualTo() less than or equal

Get Difference Between Two Dates Laravel

<?php
  
namespace App\Http\Controllers; 
use Carbon\Carbon;
   
class CarbonExampleController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $currentDateTime = Carbon::now();
        $toDateTime = Carbon::parse("2028-08-20");
  
        $diffInDays = $currentDateTime->diffInDays($toDateTime);
        $diffInMonths = $currentDateTime->diffInMonths($toDateTime);
        $diffInYears = $currentDateTime->diffInYears($toDateTime);

        dd($diffInDays,$diffInMonths,$diffInYears);
    }
}
About author
Here’s My little description of your attention on Me and My blog. I am here to help you with PHP programming.
View all posts (53)