Laravel 5 - Check if date is today

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Date Comparisons in Laravel: How to Check if a Date is Today

As developers, we frequently encounter the need to compare dates dynamically—checking if an event occurred today, within the last week, or on a specific holiday. When building dynamic timelines or dashboards, accurately flagging "today's" events requires a robust and reliable method for handling date objects in PHP and Laravel.

The challenge you are facing is very common: how do you reliably capture and compare the current date against stored dates? Relying on string formatting can lead to subtle bugs, especially when dealing with timezones or different regional formats. As senior developers, we need solutions that are not just functional but also scalable and maintainable.

This post will walk you through the most effective way to solve this problem in a Laravel environment, leveraging the power of Carbon.

Why Simple String Comparison Fails

You mentioned trying to compare dates using string formatting: @if($event['created_at']->format('d.m.Y') == *today's date*. While seemingly straightforward, this approach has several pitfalls:

  1. Format Dependency: If the format of $event['created_at'] doesn't exactly match how you format the current date, the comparison will fail unexpectedly.
  2. Timezone Issues: Dates stored in your database often have timezone information attached. Comparing simple strings ignores potential time zone shifts, leading to incorrect results if one system is operating on UTC and another on local time.
  3. Complexity: Manually managing string comparisons for date logic is error-prone compared to using dedicated object methods.

The Laravel Solution: Embracing Carbon

In the Laravel ecosystem, the gold standard for date and time manipulation is Carbon. Carbon extends PHP's native DateTime class, providing intuitive, fluent methods for handling dates. It makes complex comparisons simple and timezone-aware, which is crucial for building reliable applications, whether you are working with Eloquent models or raw data structures.

To check if a stored date matches today, we don't need to format the strings; we just need to compare the year, month, and day components directly.

Step-by-Step Implementation

First, we establish what "today" is using Carbon:

php
use Carbon\Carbon;

// 1. Get the current date for comparison (Carbon handles timezones automatically)
$today = Carbon::today();

// Assuming $event is an array holding your timeline data
// Example structure: $event = ['created_at' => '2023-10-27 14:30:00', ...]

foreach ($events as $event) {
    // 2. Convert the stored date string into a Carbon instance
    $eventDate = Carbon::parse($event['created_at']);

    // 3. Compare the date components directly
    if ($eventDate->isToday()) {
        echo "This event happened today! Display special color.";
    } else {
        echo "This event is scheduled for a different day.";
    }
}

Explanation of Best Practices

The method above utilizes the powerful isToday() method provided by Carbon. This method encapsulates all the necessary logic to compare the date part of an object against the current system date, making your code cleaner, safer, and more readable than manual string manipulation.

Notice how we convert the stored string ($event['created_at']) into a proper Carbon object using Carbon::parse(). This is vital because it ensures that all subsequent comparisons are based on standardized date objects, avoiding the pitfalls of raw string handling. When working with models in Laravel, this principle extends directly to Eloquent relationships and query building, as seen in best practices promoted by teams at laravelcompany.com.

Conclusion

Stop trying to solve date problems by manipulating strings. Adopt Carbon! By using dedicated objects like Carbon::today() and methods like isToday(), you ensure that your timeline logic is accurate, timezone-aware, and highly readable. This approach moves you from brittle string comparisons to robust object-oriented date management, which is the hallmark of high-quality Laravel development.