Laravel - How to Get Difference of Time in Minutes
Laravel - How to Get Difference of Time in Minutes

Laravel – How to Get Difference of Time in Minutes

In this tutorial, we will learn how to calculate the difference between two times in minutes using Laravel. We will use Laravel’s Carbon library, which makes it easy to work with dates and times. This guide will be simple and easy to follow, so let’s get started!

Step 1: Install Laravel

First, make sure you have Laravel installed. If you don’t have it installed, you can install it by running the following command:

composer create-project --prefer-dist laravel/laravel time-difference

Step 2: Create a Route

Next, let’s create a route in the web.php file. This file is located in the routes directory. Open the file and add the following code:

// routes/web.php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TimeDifferenceController;

Route::get('time-difference', [TimeDifferenceController::class, 'showDifference']);

Step 3: Create a Controller

Now, we need to create a controller that will handle our time difference calculation. Run the following command to create a controller named TimeDifferenceController:

php artisan make:controller TimeDifferenceController

Open the newly created controller file located in the app/Http/Controllers directory and add the following code:

// app/Http/Controllers/TimeDifferenceController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class TimeDifferenceController extends Controller
{
    public function showDifference()
    {
        // Define two times
        $startTime = Carbon::parse('2024-07-04 08:00:00');
        $endTime = Carbon::parse('2024-07-04 12:30:00');

        // Calculate the difference in minutes
        $differenceInMinutes = $startTime->diffInMinutes($endTime);

        // Return the difference
        return "The difference in minutes is: $differenceInMinutes";
    }
}

Step 4: Install Carbon

Carbon is included by default in Laravel, but if you need to install it separately, you can do so by running the following command:

composer require nesbot/carbon

Step 5: Testing the Route

To test our route, start the Laravel development server by running the following command:

php artisan serve

Open your browser and go to the following URL:

http://localhost:8000/time-difference

You should see the output showing the difference in minutes:

The difference in minutes is: 270

Explanation

  • Carbon::parse: This method is used to create a Carbon instance from a given date and time string.
  • diffInMinutes: This method calculates the difference in minutes between two Carbon instances.

Conclusion

In this tutorial, we learned how to calculate the difference between two times in minutes using Laravel’s Carbon library. This is a simple yet powerful feature that can be very useful in many applications. With just a few lines of code, we can easily work with dates and times in Laravel.

Feel free to customize the times and use this code in your own projects. Happy coding!