How to count days between two dates in PHP?

How to count days between two dates in PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

How to count days between two dates in PHP?

Calculating the number of days between two specific dates is a common requirement when building date-aware applications, whether you are tracking subscription durations or generating anniversary reminders. In PHP, this is best accomplished by converting date strings into Unix timestamps and performing simple arithmetic, rather than trying to parse calendar logic manually.

Consider two date strings stored in variables: $startDate set to "2011/07/01" and $endDate set to "2011/07/17". The goal is to determine that the interval represents 16 days of difference, or 17 days if you are counting inclusively. To achieve this reliably, you first need to ensure your input data adheres to a format PHP can easily interpret.

The most robust approach involves using strtotime() to convert both date strings into Unix timestamps. These timestamps represent the number of seconds that have elapsed since January 1, 1970, in the UTC timezone. By subtracting the timestamp of the start date from the timestamp of the end date, you get the total difference in seconds. Dividing this result by 86,400 (the number of seconds in a day) yields the precise integer difference in days.

Here is how you would implement this in a standard PHP script:

$startDate = "2011/07/01";
$endDate = "2011/07/17";

$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);

if ($startTimestamp !== false && $endTimestamp !== false) {
    $daysDifference = floor(($endTimestamp - $startTimestamp) / 86400);
    
    // Output the result
    echo "Days between dates: " . $daysDifference; 
} else {
    // Handle invalid date formats gracefully
    echo "Invalid date format detected.";
}

This method works regardless of whether the input uses slashes (/), hyphens (-), or spaces as separators. The strtotime() function is forgiving enough to handle variations like "2011-07-01" or "July 1st, 2011", making it a flexible choice for handling user input from forms or legacy databases.

However, relying solely on strtotime() introduces a subtle pitfall regarding timezones and times of day. If your date strings are meant to represent specific moments but lack time information, PHP defaults to treating them as midnight in the server's current timezone. If one date is processed in UTC and another in a localized timezone, the calculated difference might be off by several hours or even a full day depending on daylight saving time rules in that region.

To avoid these issues, it is often safer to use DateTime objects, which provide strict control over formatting and timezone handling. You can create instances using the same date strings and then calculate the difference using the diff() method:

$startDate = new DateTime($startDate);
$endDate = new DateTime($endDate);

$interval = $endDate->diff($startDate);
echo "Days between dates: " . $interval->days;

The diff() method returns a DateInterval object, and the days property holds the absolute number of days between the two points in time. This approach is generally preferred in modern PHP applications because it avoids manual arithmetic errors and handles edge cases like leap years automatically.

If you are working within the Laravel ecosystem, this logic can be encapsulated cleanly using Eloquent or helper methods available in the framework's date utilities. For instance, you might leverage the Carbon library often integrated with Laravel for more complex temporal calculations, such as determining if a specific number of days have passed since an event occurred.

Ultimately, the choice between arithmetic on timestamps and DateTime objects depends on your need for strict validation versus simple calculation. Always validate that your input strings are parsable before attempting conversion to prevent unexpected false returns or warnings in production environments.

Our network