Compare given date with today

Compare given date with today

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

Comparing Specific Dates Against Today in PHP

When working with temporal data, you often need to determine where a specific timestamp sits relative to the current moment. The source code snippet $var = "2010-01-21 00:00:00.0" represents a string formatted as a date-time value. To compare this against today's date, you must first understand that PHP treats these strings differently depending on their format and how they are processed by the date() or DateTime classes. A direct string comparison (e.g., $var < date('Y-m-d')) is fragile because it relies on lexicographical sorting rather than actual chronological value, which can break if the time components differ or if the string format isn't strictly standardized.

The most robust approach involves converting both values into objects that represent absolute points in time. The DateTime class is designed for this exact purpose. By instantiating a DateTime object from your $var and another from now(), you gain access to methods that handle timezone offsets, daylight saving time transitions, and leap years automatically.

Here is the fundamental pattern for checking if the given date is in the past, present, or future:

$givenDate = "2010-01-21 00:00:00.0";
$today = new DateTime('now');
$givenDateTime = new DateTime($givenDate);

if ($givenDateTime < $today) {
    echo "This date is in the past.";
} elseif ($givenDateTime == $today) {
    echo "This date is today.";
} else {
    echo "This date is in the future.";
}

Using strict comparison operators (<, >, ==) with DateTime objects ensures that the logic holds regardless of the internal integer representation of the timestamp. This method also handles timezones implicitly; if your application runs in UTC but the input string implies a local timezone, the DateTime constructor will respect the string's context or default to the server's timezone setting unless explicitly overridden.

A common pitfall arises when developers attempt to strip the time component before comparing. If $var contains a specific time like "2010-01-21 14:30:00" and you only want to compare against the calendar day of today, converting both to midnight (setTime(0, 0, 0)) is necessary. Otherwise, a date string for yesterday at 11:59 PM will technically be less than today at 12:00 AM, even if they share the same calendar date in your logic's intent.

Another consideration is floating-point precision when dealing with milliseconds (.ms). The provided example includes .0, which suggests a string representation often found in database outputs or JSON responses. While DateTime can parse this without issue, be cautious when mixing DateTime objects with Unix timestamps (integers). Casting a DateTime to an integer truncates the milliseconds, potentially altering comparisons if high precision is required for the specific use case.

If you are working within a Laravel application, the framework's helper functions and Eloquent models provide shortcuts that abstract this away. You can utilize Carbon, which extends DateTime, to make comparisons more readable:

use Carbon\Carbon;

$var = "2010-01-21 00:00:00.0";
$givenDate = Carbon::parse($var);
$today = Carbon::now();

if ($givenDate->isPast()) {
    // Logic for past dates
} elseif ($givenDate->isToday()) {
    // Logic for today
} else {
    // Logic for future dates
}

This approach leverages the Carbon library, which is standard in the Laravel ecosystem. It allows you to chain methods like isPast() and isToday(), reducing cognitive load and minimizing syntax errors related to operator precedence. The underlying logic remains the same—comparing immutable time values—but the interface becomes more expressive.

Ultimately, the choice between raw DateTime objects and Carbon depends on your project's stack. If you are building a standalone PHP script without Laravel, stick to the native DateTime class for performance and minimal dependencies. If you are already in a Laravel environment, Carbon offers significant convenience for date math and comparison logic. Regardless of the choice, always instantiate objects rather than comparing raw strings to ensure accuracy across different formats and timezones.

Our network