Calculate hours and minutes from date in laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Calculating Time Differences in Laravel: Mastering Hours and Minutes from Timestamps

As developers working with modern web frameworks like Laravel, managing and displaying time differences between records is a very common requirement. You often need to show users how long ago an event occurred, whether it's 'X hours and Y minutes ago'. While basic subtraction of timestamps seems intuitive, dealing with date and time objects in PHP—especially within the context of Laravel’s powerful Carbon library—requires a more structured approach to ensure accuracy and readability.

This post will dive into why direct subtraction can be tricky and show you the most robust, idiomatic way to calculate the difference in hours and minutes between a database timestamp (created_at) and the current time in your Laravel application.

The Pitfall of Simple Subtraction

When you attempt to subtract two Carbon instances (or Unix timestamps) directly using the - operator on properties like $calls['created_at'] - \Carbon\Carbon::now(), PHP handles this mathematically, resulting in a DateInterval object. While technically correct, extracting specific hours and minutes from this interval often requires extra steps, leading to verbose and sometimes error-prone code in your Blade views.

The core issue isn't the math itself, but how cleanly we want to present that difference to the end-user. We need methods designed specifically for time duration display.

The Solution: Leveraging Carbon’s diff Methods

The best practice in the Laravel ecosystem is to let Carbon handle the complex arithmetic for you. Carbon provides powerful methods like diffInHours(), diffInMinutes(), and more comprehensive methods that simplify this entire process.

To achieve exactly what you want—displaying the difference in hours and minutes—you should compare your recorded time against the current moment using Carbon’s comparison methods.

Step-by-Step Implementation

Assuming you have a record where created_at is stored as a standard timestamp (which Laravel automatically converts to a Carbon instance when retrieved from the database), here is how you calculate and display the difference in your view:

// In your Blade file, assuming $calls['created_at'] is a Carbon instance
@php
    $timeDifference = $calls['created_at']->diffForHumans(); // For human-readable output (optional)

    // Calculate raw difference in hours and minutes
    $hours = $calls['created_at']->diffInHours(now());
    $minutes = $calls['created_at']->diffInMinutes(now());
@endphp

<p>Time elapsed: {{ $hours }} hours, {{ $minutes }} minutes</p>

Note on now() vs. $calls['created_at']: When comparing two Carbon instances, you can often use the static now() method to get the current time context, or simply rely on the comparison methods which are designed to handle this relationship seamlessly. For maximum clarity and robustness, especially when dealing with timezones (a feature heavily emphasized by Laravel), always ensure both dates are properly instantiated as Carbon objects first.

Advanced Formatting for Presentation

While the method above gives you the raw numbers, you might want a more polished presentation. If you need to combine these into a single, readable string, you can use simple string interpolation:

<?php
    $createdTime = $calls['created_at'];
    $now = now();

    // Calculate the difference in minutes first for better context
    $totalMinutes = $createdTime->diffInMinutes($now);

    // Convert total minutes into hours and remaining minutes
    $hours = floor($totalMinutes / 60);
    $minutes = $totalMinutes % 60;
?>

<p>The event occurred {{ $hours }} hours and {{ $minutes }} minutes ago.</p>

This approach ensures that you are explicitly calculating the difference based on Carbon’s understanding of time, making your application more resilient to timezone shifts. For deeper dives into data handling and Eloquent relationships, exploring official Laravel documentation is always recommended. You can find excellent details on optimizing database interactions at https://laravelcompany.com.

Conclusion

Calculating time differences in Laravel is straightforward once you embrace the power of Carbon. Avoid relying solely on raw PHP subtraction for date arithmetic; instead, utilize the dedicated methods like diffInHours() and diffInMinutes(). By structuring your logic this way, you ensure that your application remains accurate, readable, and aligned with modern PHP best practices. Mastering these tools will make handling time-sensitive data in your Laravel projects significantly easier and more reliable.