Calculate difference between two dates using Carbon and Blade

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Effortlessly Calculate Dates in Laravel Using Carbon and Blade Templates Introduction: When working with dates in a Laravel application, it becomes essential to have an efficient and flexible way of handling them. One of the most popular tools for manipulating dates is the Carbon library. In this blog post, we will explore how to calculate the difference between two dates using Carbon and Blade templates. We'll also provide some best practices for working with date variables across different parts of your application. Difficulty Level: Intermediate Prerequisites: - A basic understanding of Laravel framework, including routing, controllers, and views. - Familiarity with PHP datatypes. - The Carbon PHP library installed in your project to handle date manipulation. I. Calculating Difference between Two Dates Using Carbon In this part, we will see how to create two dates from a given time string and calculate the difference in hours using the Carbon library. Step 1: Create a Time String for Toronto and Vancouver Let's assume you want to create time stamps for both Toronto and Vancouver on January 1st, 2012. For this, we can use the `createFromDate()` method provided by Carbon.

    $torontoDt = Carbon::createFromDate(2012, 1, 1, 'America/Toronto');
    $vancouverDt = Carbon::createFromDate(2012, 1, 1, 'America/Vancouver');
Step 2: Create a Time String for the Current Date We need to create a current date time object from the system time. To do this, we use `Carbon::now()` method that returns an instance of Carbon representing the current date and time.

    $currentDate = Carbon::now();
Step 3: Calculate Difference in Days between Two Dates Now, we want to find out how many days have passed from January 1st, 2012, to the current date. We can use `diffInDays()` method for this purpose. It returns the number of days that separate two given instances of Carbon.

    $testDate = $torontoDt->diffInDays($vancouverDt);
Step 4: Convert Test Date to Hours for View Output Finally, we need to convert the result from days to hours for displaying it in our blade template. We will use `format()` method of Carbon to get back only a numeric value as the number of hours that have passed.

    $testHours = round($torontoDt->diffInHours($vancouverDt));
II. Passing Dates in Views Using Blade Templates Now, we will discuss how to pass the calculated difference between dates in Carbon to a view and display it using Blade templates. Step 1: Include Your Controller Code in the Route Assuming you have created your controller method that does the calculations and assigns variables, add a route in your `web.php` file to access this function from the browser. The route should look like this:

    Route::get('/testdate', 'YourController@yourFunction');
Step 2: Retrieve and Display Test Date in Blade Template In your view, retrieve the `$testDate` and `$testHours` variables from the controller. We will also format the `$testHours` as a more readable string that includes hours, minutes, and seconds (e.g., 23:59:30).

    @php
        $days = (int) round($datework->diffInDays($now));
        $hours = (int) $testHours;
        $minutes = ($hours % 60);
        $seconds = ((($hours - floor($hours)) * 60 + $minutes) % 60);
        $formattedTime = sprintf("%d:%02d:%02d", (int)$days * 24 + $hours, $minutes, $seconds);
    @endphp

    {{ $days }}
    {{ $formattedTime }}
Conclusion: Utilizing Carbon and Blade templates can make your Laravel application more powerful when it comes to date manipulation. By following the steps in this blog post, you will be able to calculate differences between two dates, format them in a readable way, and successfully display them on your views. Remember that proper naming conventions should be followed while defining variables and using Carbon methods for clearer code readability, maintainability, and easier debugging.