How to compare two dates using laravel carbon?
This tutorial is on How to compare two dates using laravel carbon? we will help you how to compare two dates in laravel. simply and easily you can compare two dates using carbon in laravel 6, laravel 7 and laravel 8 applications. you can compare dates functions will help to check which date is smaller, bigger, or equal to two dates.
Carbon is a package that extends PHP’s own DateTime class. it provides some functionality to deal with dates in PHP. you can easily get current time easily and deal with timezones.
eq() equals
eq() function returns true if $date1 is equal to $date2.
<?php namespace App\Http\Controllers; use Carbon\Carbon; class DatecheckerController extends Controller { public function index() { $date1 = Carbon::createFromFormat('Y-m-d H:i:s', '2021-01-02 11:10:00'); $date2 = Carbon::createFromFormat('Y-m-d H:i:s', '2021-01-02 11:10:00'); $result = $date1->eq($date2); var_dump($result); } } # Output bool(true)
ne() not equals
ne() function returns true if $date1 is not equal to $date2.
<?php namespace App\Http\Controllers; use Carbon\Carbon; class DatecheckerController extends Controller { public function index() { $date1 = Carbon::createFromFormat('Y-m-d H:i:s', '2021-01-02 12:10:00'); $date2 = Carbon::createFromFormat('Y-m-d H:i:s', '2021-01-02 11:10:00'); $result = $date1->ne($date2); var_dump($result); } } # Output bool(true)
gt() greater than
gt() function returns true if $date1 is greater than $date2.
<?php namespace App\Http\Controllers; use Carbon\Carbon; class DatecheckerController extends Controller { public function index() { $date1 = Carbon::createFromFormat('Y-m-d H:i:s', '2021-01-02 12:10:00'); $date2 = Carbon::createFromFormat('Y-m-d H:i:s', '2021-01-02 11:10:00'); $result = $date1->gt($date2); var_dump($result); } } # Output bool(true)
gte() greater than or equals
<?php namespace App\Http\Controllers; use Carbon\Carbon; class DatecheckerController extends Controller { public function index() { $date1 = Carbon::createFromFormat('Y-m-d H:i:s', '2021-01-02 11:10:00'); $date2 = Carbon::createFromFormat('Y-m-d H:i:s', '2021-01-02 11:10:00'); $result = $date1->gte($date2); var_dump($result); } } # Output bool(true)
lt() less than
lt() function returns true if $date1 is less than $date2.
<?php namespace App\Http\Controllers; use Carbon\Carbon; class DatecheckerController extends Controller { public function index() { $date1 = Carbon::createFromFormat('Y-m-d H:i:s', '2021-01-02 08:10:00'); $date2 = Carbon::createFromFormat('Y-m-d H:i:s', '2021-01-02 10:10:00'); $result = $date1->ls($date2); var_dump($result); } } # Output bool(true)
lte() less than or equals
lte() function Returns true if $date1 is less than or equal to $date2.
<?php namespace App\Http\Controllers; use Carbon\Carbon; class DatecheckerController extends Controller { public function index() { $date1 = Carbon::createFromFormat('Y-m-d H:i:s', '2021-01-02 08:10:00'); $date2 = Carbon::createFromFormat('Y-m-d H:i:s', '2021-01-02 08:10:00'); $result = $date1->lte($date2); var_dump($result); } } # Output bool(true)