How to Compare Dates with PHP Carbon in Laravel

Laravel PHP

When working with dates in Laravel, the Carbon library is your best companion. It offers a clean, intuitive API for handling and comparing dates and times. In this article, we will explore several practical ways to compare dates using Carbon in a Laravel project.

What is Carbon?

Carbon is a PHP extension for working with dates. Laravel includes Carbon out of the box, providing easy and expressive date/time handling.

Basic Date Comparison

You can compare dates using relational operators or Carbon’s built-in methods.

use Carbon\Carbon;

$date1 = Carbon::parse('2024-04-01');
$date2 = Carbon::parse('2024-04-10');

// Using greater than or less than
if ($date1 < $date2) {
    echo "$date1 is earlier than $date2";
}

// Using Carbon's comparison methods
if ($date1->lt($date2)) {
    echo "$date1 is less than $date2";
}

Comparison Methods

Carbon provides intuitive methods for comparisons:

  • lt() – less than
  • lte() – less than or equal
  • gt() – greater than
  • gte() – greater than or equal
  • eq() – equal
  • ne() – not equal
$date1->gt($date2); // returns false
$date1->lte($date2); // returns true

Checking if Two Dates Are the Same Day

Use the isSameDay() method to check if two Carbon instances refer to the same calendar day.

$date1 = Carbon::parse('2024-04-01 12:00:00');
$date2 = Carbon::parse('2024-04-01 23:59:59');

if ($date1->isSameDay($date2)) {
    echo "These dates are on the same day.";
}

Comparing Only Dates (Ignoring Time)

Use the toDateString() method to compare only the date portion:

if ($date1->toDateString() === $date2->toDateString()) {
    echo "Same calendar day";
}

Alternatively, you can use the isSameDay() method as shown above.

Date Difference Calculation

You can calculate the difference between two dates:

$diffInDays = $date1->diffInDays($date2); // 9
$diffInHours = $date1->diffInHours($date2); // 216

Use diffIn...() methods for flexible comparisons:

  • diffInYears()
  • diffInMonths()
  • diffInDays()
  • diffInHours()

You can also specify if the difference should be absolute or signed:

$date1->diffInDays($date2, false); // negative if $date1 > $date2

Checking if a Date Is in the Past or Future

$date = Carbon::parse('2024-04-01');

if ($date->isPast()) {
    echo "The date has passed.";
}

if ($date->isFuture()) {
    echo "The date is in the future.";
}

Use Cases in Real Projects

  • Comparing deadlines and due dates.
  • Checking if a user’s subscription has expired.
  • Determining whether a coupon code is still valid.
  • Logging and filtering records by date range.

Best Practices

  • Always ensure your timezones are correctly set when comparing timestamps.
  • Prefer Carbon methods (isSameDay()gt(), etc.) over raw operators for better readability.
  • Use diffInDays() or similar methods for user-friendly displays like “9 days left.”

Summary

Carbon makes date comparison in Laravel not only powerful but also highly readable. By using the methods introduced above, you can manage time-sensitive logic effectively and cleanly.

For more on Carbon, visit the official documentation.


Looking for more Laravel tips? Browse our Laravel category for more tutorials and cheat sheets!

著者

30 years of experience as a web engineer, currently working as a freelance backend engineer.

PHP: ~30 years (Laravel 7 years / FuelPHP 5 years / CakePHP / custom frameworks)
JavaScript: ~20 years (React & Vue, 4 years each)
Cloud & Infrastructure: AWS (EC2, CloudFront, RDS, API Gateway, etc.) / GCP (BigQuery)

I have been writing PHP since version 4, back when the framework ecosystem was fragmented
and every team had their own approach. I've lived through the evolution firsthand —
from raw PHP and homegrown frameworks to the modern Laravel era —
which means I don't just know how to use a tool, but why it exists and what problem it replaced.

I work across system design, implementation, and operations, primarily on backend systems
for both product companies and contract-based projects.

On this blog, I write about the things I actually got stuck on, looked up, or figured out
in real-world projects. If something here unblocks even one person's day, that's enough for me.

千原 耕司をフォローする

役にたったと思ったら応援をお願いします m(._.)m

PHP
スポンサーリンク
シェアする
千原 耕司をフォローする
Copied title and URL