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

eq() or equalTo()

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

ne() or notEqualTo()

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.


Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.