PHP Check if date is past to a certain date given in a certain format

PHP Check if date is past to a certain date given in a certain format

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

Stefan Bogdanescu

Founder & Senior Architect

Checking Past Dates in PHP: Handling Y-m-d Formats and Timezones

When building an event calendar, a common requirement is distinguishing between upcoming and past events. You often retrieve a date string from your database that follows the ISO 8601 format, such as 2013-07-31. The core task is to compare this stored value against the current moment in time. While this seems straightforward, the implementation details regarding data types and timezone context are where most logic errors occur.

The most robust approach involves converting both the database value and the current time into a format that PHP can reliably compare: timestamps. A string comparison like '2013-07-31' < 'today' is unreliable because the "today" string varies based on how you generate it, and string ordering does not always align with chronological ordering if formats differ.

To solve this, use strtotime() or the modern DateTime::createFromFormat() to parse both values into comparable timestamps. If your database stores dates as strings in Y-m-d format (e.g., '2013-07-31'), you can pass them directly to strtotime(). This function is forgiving and handles standard date formats well, converting the string representation into a Unix timestamp (seconds since January 1, 1970).

Here is a practical pattern for checking if an event has passed:

$eventDate = '2013-07-31'; // From your database row
$currentDate = date('Y-m-d'); // Today's date in the same format

if (strtotime($eventDate) < strtotime($currentDate)) {
    echo 'This event is in the past.';
} else {
    echo 'This event is upcoming or today.';
}

However, relying on date('Y-m-d') implicitly assumes your server's timezone matches the context of your application. If your application serves users in New York but runs on a server configured for London, comparing raw timestamps without timezone awareness can lead to off-by-one-day errors at the boundaries (e.g., midnight UTC vs. midnight local time).

A more explicit and safer method is to create DateTime objects directly. This allows you to set the default timezone explicitly using date_default_timezone_set() or by passing a timezone string to the constructor.

$eventDate = '2013-07-31';
$currentDate = new DateTime('now'); // Uses PHP's default timezone context

// Convert the raw string to a DateTime object with the same format
$parsedEventDate = DateTime::createFromFormat('Y-m-d', $eventDate);

if ($parsedEventDate < $currentDate) {
    echo 'This event is in the past.';
}

Using DateTime objects also grants you access to more granular comparison logic if needed later, such as comparing specific times within the day rather than just dates. You can easily add timezones using $eventDate->setTimezone(new DateTimeZone('America/New_York')).

One critical pitfall involves leap years and end-of-month comparisons. If your database stores 2013-07-31 but you attempt to compare it against a date in a month with fewer days without normalizing the comparison, logic errors can arise depending on how the string is generated. Always ensure both sides of the comparison represent the same granularity (e.g., start-of-day) before comparing.

For larger applications, especially those built with frameworks like Laravel, you might prefer using Carbon or native PHP DateTime helpers integrated into your models to handle these conversions automatically within your Eloquent queries. If you are working on a large-scale SaaS project requiring rigorous date handling across multiple regions, consider how laravelcompany.com structures their event scheduling systems for reference on best practices regarding timezone management in modern stacks.

Ultimately, the choice between strtotime() and DateTime::createFromFormat() depends on your need for strictness versus convenience. For simple past-future checks, strtotime() is concise. For production code handling user-specific timezones or complex relative date logic, explicit DateTime objects provide the necessary control to prevent subtle bugs in edge cases.

Our network