Laravel Carbon Compare Two Dates without Times
Comparing dates is super easy in Laravel with Carbon. Carbon has many built-in functions to compare dates. First, let's format 2 dates without times:
$input_date = Carbon::createFromFormat('Y-m-d', $date);
$today = Carbon::createFromFormat('Y-m-d', date('Y-m-d'));
The dates are equal:
// using eq()
if ($input_date->eq($today)) {
dd('Dates are equal.');
} else {
dd('Dates are not equal.');
}
// using equalTo()
if ($input_date->equalTo($today)) {
dd('Dates are equal.');
} else {
dd('Dates are not equal.');
}
The dates are not equal:
// using ne()
if ($input_date->ne($today)) {
dd('Dates are not equal.');
} else {
dd('Dates are equal.');
}
// using notEqualTo()
if ($input_date->notEqualTo($today)) {
dd('Dates are not equal.');
} else {
dd('Dates are equal.');
}
Please check this doc to know more about date comparison functions.

Md Obydullah
https://shouts.dev/obydul
Comments
No comments yet…