ItSolutionStuff.com

Laravel Carbon addHours() | Laravel Carbon Add Hours Example

By Hardik Savani • April 16, 2024
Laravel

Hello,

I am going to show you example of laravel carbon add hour. if you have question about laravel carbon add hours then i will give simple example with solution. i explained simply step by step laravel carbon add 1 hour. i would like to show you laravel carbon addHours().

You can add hours on current date using carbon in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

If you need to add hour or more hours in date then you can use carbon in laravel. carbon provide addHour() and addHours() method to add hours on carbon date object. so let's see some examples to adding hour and hours and sub hour and years from date.

Let's see example:

Example 1: Add Hour

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Carbon\Carbon;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$currentDateTime = Carbon::now();

$newDateTime = Carbon::now()->addHour();

print_r($currentDateTime);

print_r($newDateTime);

}

}

Output

Carbon\Carbon Object

(

[date] => 2020-11-05 04:29:35.435461

[timezone_type] => 3

[timezone] => UTC

)

Carbon\Carbon Object

(

[date] => 2020-11-05 05:29:35.435461

[timezone_type] => 3

[timezone] => UTC

)

Example 2: Add Hours

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Carbon\Carbon;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$currentDateTime = Carbon::now();

$newDateTime = Carbon::now()->addHours(5);

print_r($currentDateTime);

print_r($newDateTime);

}

}

Output

Carbon\Carbon Object

(

[date] => 2020-11-05 04:29:35.435461

[timezone_type] => 3

[timezone] => UTC

)

Carbon\Carbon Object

(

[date] => 2020-11-05 09:29:35.435461

[timezone_type] => 3

[timezone] => UTC

)

Example 3: Sub Hour

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Carbon\Carbon;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$currentDateTime = Carbon::now();

$newDateTime = Carbon::now()->subHour();

print_r($currentDateTime);

print_r($newDateTime);

}

}

Output

Carbon\Carbon Object

(

[date] => 2020-11-05 04:32:50.651145

[timezone_type] => 3

[timezone] => UTC

)

Carbon\Carbon Object

(

[date] => 2020-11-05 03:32:50.651145

[timezone_type] => 3

[timezone] => UTC

)

Example 4: Sub Hours

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Carbon\Carbon;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$currentDateTime = Carbon::now();

$newDateTime = Carbon::now()->subHours(2);

print_r($currentDateTime);

print_r($newDateTime);

}

}

Output

Carbon\Carbon Object

(

[date] => 2020-11-05 04:29:51.651667

[timezone_type] => 3

[timezone] => UTC

)

Carbon\Carbon Object

(

[date] => 2020-11-05 02:29:51.651667

[timezone_type] => 3

[timezone] => UTC

)

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Carbon addYears() | Laravel Carbon Add Year Example

Read Now →

Laravel Carbon Subtract Months from Date Example

Read Now →

Laravel Carbon addMonths() | Laravel Carbon Add Months Example

Read Now →

Laravel Carbon Subtract Days to Date Example

Read Now →

Laravel Carbon Add Days to Date Example

Read Now →

Laravel 8 Send Mail using Queue Example

Read Now →

Angular Material Card Example | Angular mat-card

Read Now →

Laravel Calculate Age from Date of Birth Example

Read Now →

Laravel Carbon Get Year, Month and Day from Timestamp Example

Read Now →

Laravel Change Date Format using Carbon Example

Read Now →

How to Get Hours Difference Between Two Dates in Laravel?

Read Now →

How to Get Month Difference Between Two Dates in Laravel?

Read Now →