Date Difference in php on days?

Date Difference in php on days?

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

Stefan Bogdanescu

Founder & Senior Architect

Calculating Date Differences in PHP: A Practical Approach

You often need to determine the gap between two timestamps, whether you are building a subscription expiry checker or calculating user tenure. If you have two ISO 8601 strings like 2009-11-12 12:09:08 and 2009-12-01 08:20:11, the most direct way to get the result in days is to convert both strings into Unix timestamps using strtotime() or DateTime, subtract the values, and then divide by the number of seconds in a day.

This method relies on the fact that PHP's internal date representation is fundamentally tied to Unix time: the number of seconds elapsed since January 1, 1970, 00:00:00 UTC. By converting both dates to this integer representation, you strip away the complexity of calendar months and leap years, leaving a simple arithmetic problem.

Here is how you would perform that calculation in modern PHP 8.x using DateTime:

$date1 = '2009-11-12 12:09:08';
$date2 = '2009-12-01 08:20:11';

$datetime1 = new DateTime($date1);
$datetime2 = new DateTime($date2);

// Calculate difference in seconds
$differenceInSeconds = $datetime2->getTimestamp() - $datetime1->getTimestamp();

// Convert to days (24 hours * 60 minutes * 60 seconds)
$differenceInDays = floor($differenceInSeconds / (86400));

echo $differenceInDays; // Outputs: 19

The logic here is sound because DateTime::getTimestamp() returns an integer. Subtracting the two integers gives you the exact duration in seconds, accounting for any time zone differences if your timestamps were created with specific offsets. Since standard string parsing assumes local or UTC depending on configuration, ensure both dates are in the same timezone before comparing them to avoid off-by-one errors caused by daylight saving time transitions.

You might notice that using floor() is critical here. If you simply cast the result to an integer or use (int), you get the same effect as floor for positive numbers, but it truncates toward zero. While mathematically equivalent in this specific context where $date2 > $date1, being explicit with floor() clarifies your intent: you are discarding fractional days and keeping only the complete 24-hour periods that have passed.

An alternative approach involves creating a DateInterval object directly from one DateTime to another. This is useful if you need more than just raw days. The DateInterval class encapsulates the difference between two dates, providing properties like days, hours, and minutes.

$interval = $datetime2->diff($datetime1);
echo $interval->days; 
// Outputs: 19

This method is often preferred in frameworks because it abstracts away the arithmetic entirely. However, if you strictly need the total number of days including partial ones (e.g., treating a 10-hour difference as roughly 0.4 days), DateInterval will truncate those values unless you manually convert them back to seconds. For pure day counts where precision beyond whole days is unnecessary, DateInterval offers cleaner code without manual timestamp manipulation.

When dealing with legacy PHP versions or environments where DateTime might not be available, the strtotime() function serves as a reliable fallback. You can pass the string directly to strtotime(), which returns a Unix timestamp. The arithmetic remains identical: subtract the two results and divide by 86400. Just be aware that strtotime() is less predictable with complex relative dates (like "next Friday") compared to DateTime objects, so for strict date math, prefer explicit formatting strings like ISO 8601.

If you are working within a Laravel application, the framework provides a fluent interface via the Carbon package that handles these calculations elegantly. Carbon extends PHP's native DateTime class and adds methods specifically designed for time delta operations. You can use the diffInDays() method to get exactly what you need without writing the arithmetic yourself.

use Illuminate\Support\Carbon;

$date1 = Carbon::parse('2009-11-12 12:09:08');
$date2 = Carbon::parse('2009-12-01 08:20:11');

echo $date2->diffInDays($date1); // Outputs: 19

While this adds a dependency on the Carbon package, it significantly reduces the chance of implementation errors and makes the code more readable. For projects not using Laravel, sticking to native DateTime or DateInterval is sufficient and avoids unnecessary bloat.

Ultimately, calculating date differences in PHP comes down to understanding that dates are just numbers under the hood. Whether you choose the raw arithmetic with timestamps or the higher-level diff() methods, the key is ensuring your inputs are normalized to the same timezone context before performing the subtraction.

Our network