How to get time difference in minutes in PHP

How to get time difference in minutes in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

How to get time difference in minutes in PHP

Calculating the time difference between two datetime values is a frequent requirement when building applications that track sessions, log events, or manage scheduled tasks. While it might seem trivial to simply subtract two timestamps, doing so correctly requires understanding how PHP represents time internally and handling edge cases like negative durations. The goal is to get an integer representing minutes, but the approach depends heavily on whether you are using native PHP functions or a framework like Laravel.

In standard PHP, the most robust method involves converting your datetime objects into Unix timestamps (seconds since the epoch) before performing arithmetic. This ensures that timezone offsets do not interfere with the calculation if both datetimes are interpreted in the same context. The DateTime class provides an excellent way to handle this without manual string parsing.

$start = new DateTime('2023-10-25 14:30:00');
$end = new DateTime('2023-10-25 16:45:00', new DateTimeZone('UTC'));

// Convert to Unix timestamps (seconds)
$startTimestamp = $start->getTimestamp();
$endTimestamp = $end->getTimestamp();

// Calculate difference in seconds, then divide by 60 for minutes
$diffSeconds = abs($endTimestamp - $startTimestamp);
$minutes = round($diffSeconds / 60);

echo $minutes; // Outputs: 135

The use of abs() is critical here. Depending on the order in which you pass your variables, $endTimestamp could be smaller than $startTimestamp, resulting in a negative number. If your application logic expects a duration regardless of direction (e.g., "how long ago was this?"), taking the absolute value prevents errors downstream when dividing by 60 or passing the result to functions that expect non-negative integers.

If you are working within the Laravel ecosystem, the Carbon library offers a more expressive syntax that abstracts away the timestamp conversion. This is particularly useful when readability takes precedence over raw performance in standard web requests. Carbon handles the datetime math internally and returns a DateInterval object or an integer directly.

use Carbon\Carbon;

$start = Carbon::parse('2023-10-25 14:30:00');
$end = Carbon::parse('2023-10-25 16:45:00', 'UTC');

// Direct calculation returning difference in minutes
$minutes = abs($start->diffInMinutes($end));

dd($minutes); // Outputs: 135

One common pitfall occurs when dealing with different timezones. If $start is defined without a timezone (defaulting to DateTimeZone default or UTC depending on environment) and $end explicitly sets America/New_York, the calculation will account for the offset difference, not just the wall-clock hours. In most business logic scenarios, you should ensure both instances are normalized to the same timezone before subtraction to avoid confusion between local time and UTC.

For high-performance contexts where every millisecond counts, native PHP's DateTime with getTimestamp() remains slightly faster than Carbon because it avoids the overhead of the external library loading and object instantiation. However, for typical web applications, the readability gain from Carbon often outweighs the negligible performance difference.

If you prefer to work with string inputs directly, using DateTime::createFromFormat ensures that your parsing logic is explicit rather than relying on implicit format guessing. This approach reduces ambiguity but requires a strict adherence to the format string provided in the second argument of the constructor.

$startTime = DateTime::createFromFormat('Y-m-d H:i:s', '2023-10-25 14:30:00');
$endTime   = DateTime::createFromFormat('Y-m-d H:i:s', '2023-10-25 16:45:00');

if ($startTime && $endTime) {
    $minutes = abs($endTime->getTimestamp() - $startTime->getTimestamp()) / 60;
} else {
    // Handle parsing failure gracefully
}

Finally, consider how you intend to use the result. If you need to display this duration to a user, formatting it as "2 hours and 15 minutes" might be more intuitive than raw minutes. For backend logic comparing durations, the integer minute count is usually sufficient. Always validate your inputs and handle exceptions if the strings passed to the constructors are malformed, as uncaught exceptions can halt request execution in production environments.

Our network