Carbon Add Hours To Date In Laravel 9

Websolutionstuff | Nov-22-2022 | Categories : Laravel PHP

In this article, we will see carbon add hours to date in laravel 9. Carbon provides addHour and addHours() functions to add an hour to date object. Using addHour() function you can add an hour to time and addHours() function may help to add hours to date. but in the addHours() function needs to pass the no. of hours as a parameter.

So, let's see, how to add an hour in laravel 9, PHP carbon add an hour, add hours in laravel 9

Carbon addHour() Function

 In this example, we will add an hour to the current date using the carbon addHour() function.

<?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] => 2022-11-03 09:04:30

    [timezone_type] => 2

    [timezone] => GMT
)

Carbon\Carbon Object
(
    [date] => 2022-11-03 10:04:30

    [timezone_type] => 2

    [timezone] => GMT
)

 

 

Carbon addHours() Function

In this example, we will add 7 hours to the current date using the carbon addHours() function.

<?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(7);
             
        print_r($currentDateTime);
        print_r($newDateTime);
    }
}
 

 

Output: 

Carbon\Carbon Object
(
    [date] => 2022-11-03 09:05:45

    [timezone_type] => 2

    [timezone] => GMT
)

Carbon\Carbon Object
(
    [date] => 2022-11-03 16:05:45

    [timezone_type] => 2

    [timezone] => GMT
)

 

 

Carbon diffForHumans with addHour() Function

In this example, we will use the diffForHumans() function with addHour() 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 = $currentDateTime->diffForHumans($currentDateTime->copy()->addHour());
             
        print_r($currentDateTime);
        print_r($newDateTime);
    }
}

 

Output:

2022-11-03 09:10:46
1 hour before

 

Carbon diffForHumans with addHours() Function

In this example, we will add 7 hours to the current date using the diffForHumans() function with the addHours() function.

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

 

Output:

2022-11-03 09:11:11
7 hours before

 


You might also like:

Recommended Post
Featured Post
How To File Upload In React JS
How To File Upload In React JS

In this article, we will see how file uploads in react js. File uploading means a user from a client machine wants...

Read More

Sep-05-2022

Laravel 9 Vue JS CRUD Operation Example
Laravel 9 Vue JS CRUD Operatio...

In this article, we will see the laravel 9 vue js crud operation example. Here, we will learn how to create a vue 3...

Read More

Dec-07-2022

How to Create Payment Link in Stripe using API in Laravel 10
How to Create Payment Link in...

In today's digital age, the ability to facilitate online payments efficiently is crucial for businesses and develope...

Read More

Oct-09-2023

How to Image Upload Laravel 11 Vue 3 Example
How to Image Upload Laravel 11...

Hello, laravel web developer! In this article, we'll see how to image upload in laravel 11 vue 3. Here, we'...

Read More

May-27-2024