Get interval seconds between two datetime in PHP?

Get interval seconds between two datetime in PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

Calculating Duration Between Datetime Strings in PHP

You have two specific timestamps: 2009-10-05 18:11:08 and 2009-10-05 18:07:13. The goal is to determine the difference between them, which equals 235 seconds (4 minutes and 55 seconds). While this seems like a simple arithmetic problem, handling datetime strings in PHP requires converting them into a format that supports precise time delta calculations.

The most robust approach involves using DateTime objects or the legacy strtotime() function. Both methods convert human-readable date strings into Unix timestamps (integers representing seconds since January 1, 1970). Once you have two integers, subtraction yields the difference in seconds directly.

Using DateTime Objects

The DateTime class is the modern standard for handling time in PHP. It offers better precision and fewer edge cases compared to string manipulation or legacy functions.

To calculate the interval, first instantiate a DateTime object for each string:

$date1 = new DateTime('2009-10-05 18:11:08');
$date2 = new DateTime('2009-10-05 18:07:13');

// Calculate the difference
$interval = $date1->diff($date2);

echo $interval->i * 60 + $interval->s; // Output: 235

The DateTime::diff() method returns a DateInterval object. This object contains properties for years, months, days, hours, minutes, and seconds ($interval->y, $interval->m, etc.). However, you must be careful when interpreting these values. The DateInterval class does not automatically normalize the interval into a single unit like "total seconds" unless you manually aggregate the components.

In the example above, we multiply the minutes by 60 and add the remaining seconds to get the total duration in seconds. This manual aggregation is necessary because the underlying logic of diff() treats months as variable-length periods (28–31 days) rather than a fixed count of 30 days or 30.44 days. If your dates cross month boundaries, relying solely on $interval->s will give you an incomplete picture unless you account for the larger time units.

Using strtotime()

If you are working in an older codebase or prefer brevity, strtotime() is a viable alternative. It accepts various date formats and returns a Unix timestamp (an integer).

$timestamp1 = strtotime('2009-10-05 18:11:08');
$timestamp2 = strtotime('2009-10-05 18:07:13');

$differenceInSeconds = $timestamp1 - $timestamp2;
echo $differenceInSeconds; // Output: 235

This approach is often cleaner for simple calculations because it bypasses the DateInterval object entirely. You get a raw integer representing the exact number of seconds between the two moments. However, be aware that strtotime() relies on server locale settings and can sometimes misinterpret ambiguous date formats (e.g., "tomorrow" or relative time strings) unless strictly formatted dates are provided.

Important Pitfalls and Trade-offs

A common mistake when working with DateTime::diff() is assuming the interval represents a contiguous block of seconds across different units without normalization. For instance, if you calculate the difference between October 5th and October 6th using diff(), the $interval->d property will be 1. But if you simply sum $interval->s, you might miss the contribution of days to the total second count unless you explicitly convert days to seconds ($interval->days * 86400).

When dealing with data stored in a database, ensure your columns are cast as datetime or timestamp. Attempting to subtract two raw strings directly (e.g., $str1 - $str2) will result in an error or incorrect output because PHP does not perform lexical comparison on date strings for time duration.

For complex applications involving Laravel, the framework provides a Carbon library that extends DateTime with human-readable methods and fluent interfaces. While Carbon is powerful, it introduces a dependency. If your project requires zero dependencies, sticking to the built-in DateTime class or strtotime() ensures maximum compatibility and minimal overhead. The core logic of subtracting Unix timestamps remains consistent regardless of the wrapper used.

Final Recommendation

For most scenarios requiring interval seconds between two specific datetime strings, converting them to Unix timestamps via either DateTime or strtotime() is the most reliable strategy. It abstracts away the complexities of calendar arithmetic and provides an immediate integer result that can be used in further calculations, such as formatting durations with DateInterval::format() or sending precise metrics to a logging service.

Our network