Carbon Add Hours In Laravel

Websolutionstuff | Dec-09-2020 | Categories : Laravel PHP

In this article, we will see examples of carbon add hours in laravel 8. Carbon provides many functions like addHour() and addHours() to add hours in laravel 7/8. You can add an hour on the current date using carbon in laravel 7 and laravel 8. Also, you can add hours to date using the carbon addHours() function.

The addHours() function may require to passing the parameter of the number of hours. So, you can add hours to any date object.

So, let's see how to add hours to date in laravel 7/8 and laravel 7/8 add hour to date using carbon.

Carbon addHour() Example

In this example, we will add an hour to the current date.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DateController extends Controller
{
    public function index()
    {
        $currentDateTime = Carbon::now();
        $newDateTime = Carbon::now()->addHour();
             
        print_r($currentDateTime);
        print_r($newDateTime);
    }
}

 

Output:

Carbon\Carbon Object
(
    [date] => 2020-12-07 09:45:30.376461

    [timezone_type] => 2

    [timezone] => GMT
)

Carbon\Carbon Object
(
    [date] => 2021-12-07 10:45:30.376461

    [timezone_type] => 2

    [timezone] => GMT
)

 

 

Carbon addHours() Example

In this example, we will add hours to the current date. Also, you can add hours to any date.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DateController extends Controller
{
    public function index()
    {
        $currentDateTime = Carbon::now();
        $newDateTime = Carbon::now()->addHours(3);
             
        print_r($currentDateTime);
        print_r($newDateTime);
    }
}
 

 

Output:

Carbon\Carbon Object
(
    [date] => 2020-12-07 09:46:32.406561

    [timezone_type] => 2

    [timezone] => GMT
)

Carbon\Carbon Object
(
    [date] => 2023-12-07 12:46:50.454561

    [timezone_type] => 2

    [timezone] => GMT
)

 


You might also like:

Recommended Post
Featured Post
How To Increase Session Lifetime In Laravel
How To Increase Session Lifeti...

In this article, we will see how to increase session timeout in laravel. In this example, we can see how to se...

Read More

Jun-28-2020

How To Generate Barcode In Laravel
How To Generate Barcode In Lar...

In this tutorial, I will show you how to generate barcodes using the milon/barcode package. In this example, we wil...

Read More

Jun-06-2020

How To Get Client IP Address In Laravel 9
How To Get Client IP Address I...

In this article, we will see how to get a client's IP address in laravel 9. Many times you need a user IP addre...

Read More

Oct-26-2022

Laravel 9 Many To Many Polymorphic Relationship
Laravel 9 Many To Many Polymor...

In this article, we will see laravel 9 many to many polymorphic relationship. many to many polymorphic relationship more...

Read More

Apr-06-2022