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