Laravel Carbon Add Hours Tutorial with Example

Last Updated on by in Laravel

Throughout this tutorial, you will learn how to add an hour, add hours, add sub hour, and add sub hours in Laravel 9 application with the help of the PHP Carbon package.

This Laravel Carbon tutorial will clarify brevity, although this Laravel add hour example is built with Laravel 9; however, you can use the similar approach in the older versions like Laravel 7 | 6.

When it comes to handling date and time in PHP-based frameworks, then you can rely on Carbon module with your eyes closed on; it is powered by PHP and extends the PHP’S date-time class.

Ideally, when you develop an application, you often have to deal with date-time. Consequently, we will share with you how to add an hour, hours, sub hour, and sub hours on a current or existing date with the help of Carbon package in Laravel project.

Enough explanation, let’s get to the point.

Getting Started

If Laravel app is not installed then use the below command otherwise skip this step:

composer create-project laravel/laravel --prefer-dist laravel-carbon-tutorial

Get into the app:

composer create-project laravel/laravel --prefer-dist laravel-carbon-tutorial

Start the application:

php artisan serve

Open the app to see the results on the browser:

http://127.0.0.1:8000

Afterwards, you need a controller to add hour and sub hour in Laravel application:

php artisan make:controller DateTimeController

All the below code will go to app/Http/Controllers/DateTimeController.php file:

Define Hour with addHour()

This is the simplest example, in the below example we are adding hour and aligning in the controller by defining a addHours() function.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DateTimeController extends Controller
{
    // view
    public function index()
    {
        return view('home');
    }

    // define hour
    public function addHour()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addHour();
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

}

Define Hours with addHours()

Add hours and pass the number of hours in the method, all is happening with the help of Carbon class that we added on the top of the controller.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DateTimeController extends Controller
{
    // view
    public function index()
    {
        return view('home');
    }


    // define hours
    public function addHours()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addHours(6);
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

}

Define Sub Hour with subHour()

It is as simple as we added the hour but this time you have to call the subHour() method of Carbon object.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DateTimeController extends Controller
{
    // view
    public function index()
    {
        return view('home');
    }

    // define sub hour
    public function addSubHour()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->subHour();
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

}

Define Sub Hours with subHours()

Eventually, you can add sub hours by passing the hours values in subHours() method:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DateTimeController extends Controller
{
    // view
    public function index()
    {
        return view('home');
    }

    // define sub hours
    public function addSubHours()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->subHours(3);
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }    
}

Fret not; here is the final conjugated code:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DateTimeController extends Controller
{
    // view
    public function index()
    {
        return view('home');
    }

    // define hour
    public function addHour()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addHour();
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

    // define hours
    public function addHours()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addHours(6);
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

    // define sub hour
    public function addSubHour()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->subHour();
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

    // define sub hours
    public function addSubHours()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->subHours(3);
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }    
}

Summary

Ultimately, we saw how to add hours and sub hours with the Carbon module in the Laravel application.

I believe this will help you to deal with date and time in the near future, i assume you would love the brevity of this tutorial.