PHP check if date between two dates

PHP check if date between two dates

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

Stefan Bogdanescu

Founder & Senior Architect

PHP check if date between two dates

Comparing dates in PHP often trips up developers because of how string comparison differs from timestamp arithmetic. The code snippet you encountered attempts to solve this by formatting dates as d/m/Y strings and then using standard operators like > and <. While this approach might seem logical at first glance, it fails silently and produces incorrect results due to the nature of lexicographical sorting on date strings.

When you execute $paymentDate = date('d/m/Y');, PHP returns a string like "25/10/2023". Similarly, your start and end dates become "01/01/2001" and "01/01/2015". The condition if ($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd) performs a string comparison, not a chronological one.

Consider the logic behind the failure. If today is October 25, 2023, the string is "25/10/2023". The start date string is "01/01/2001". In ASCII sorting, characters are compared from left to right. The character '2' (from the day) has a higher value than '0' (from the year), so "25/10/2023" is indeed greater than "01/01/2001". However, compare this to an end date of "01/01/2015". The string "25/10/2023" starts with '2', while "01/01/2015" starts with '0'. Lexicographically, "2..." is greater than "0...", so the check passes.

The problem arises when comparing dates where the day or month values change significantly but the year remains constant, or when crossing century boundaries in a format that doesn't account for them properly. More critically, if your end date was formatted as Y/m/d (e.g., "2015/01/01"), the comparison would break immediately because '2' (from 25) is less than '2' (from 20), but then you compare the next character. Wait, actually, let's look closer at a specific failure case.

Imagine checking if "25/10/2023" is between "01/01/2010" and "31/12/2012".
String A: "25/10/2023"
String B (End): "31/12/2012"

Comparing A and B: The first character is '2' vs '3'. Since '2' < '3', the string A is considered smaller than String B. So far, so good. But what if the end date was "05/06/2024"?
String A: "25/10/2023"
String C (End): "05/06/2024"

First char: '2' vs '0'. '2' > '0', so A > C. The check A < C fails. Even though October 2023 is chronologically before June 2024, the string comparison says otherwise because it prioritizes the day number ('25') over the year ('2023' vs '2024'). String comparison relies on the first differing character to determine order, ignoring the semantic meaning of which part of the date represents the year.

The Reliable Solution: Unix Timestamps

To perform accurate date comparisons in PHP, you must convert both strings into a numeric format that represents time linearly. The most robust way to do this is using the Unix timestamp. The strtotime() function parses a human-readable date string and returns the number of seconds since January 1, 1970.

Here is the corrected implementation:

$paymentDate = new DateTime(); // Get today's date as an object
$contractDateBegin = '01/01/2001';
$contractDateEnd = '01/01/2015';

// Convert string dates to timestamps
$beginTimestamp = strtotime($contractDateBegin);
$endTimestamp = strtotime($contractDateEnd);
$currentTimestamp = $paymentDate->getTimestamp();

if ($currentTimestamp > $beginTimestamp && $currentTimestamp < $endTimestamp) {
    echo "Payment date is within contract period.";
} else {
    echo "Payment date is outside contract period.";
}

Alternatively, using the DateTime class directly avoids manual timestamp conversion and handles leap years and timezone nuances automatically:

$paymentDate = new DateTime();
$contractDateBegin = new DateTime('01/01/2001');
$contractDateEnd = new DateTime('01/01/2015');

if ($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd) {
    echo "is between";
} else {
    echo "NO GO!";
}

This approach ensures that the comparison is strictly chronological. The DateTime class compares the internal integer representation of the date/time, guaranteeing accuracy regardless of how the input strings are formatted.

Why This Matters in Production

In financial applications or subscription logic, relying on string sorting for dates is a critical bug waiting to happen. It can lead to incorrect billing periods, invalid warranty checks, or security vulnerabilities if access control relies on date ranges that fail silently. Always normalize dates into objects or timestamps before performing arithmetic or range checks. If you are building complex date logic in PHP, frameworks like Laravel provide helpers such as now(), today(), and Carbon instances that abstract these complexities further, ensuring your date handling remains consistent across the application stack.

Our network