DEV Community

loading...

Carbon Add Hours In Laravel

websolutionstuff profile image Websolutionstuff Originally published at websolutionstuff.com ・2 min read

Today we will see example of carbon add hours in laravel, here i will give you simple example of laravel carbon add hours, carbon provides many function like addHour(), addHours() to add hour in laravel.

If we need to add hour or more then one hours in date and time then you can use carbon in laravel. carbon provides addHour() and addHours() method to add hours on carbon date object.

addHour() Example

<?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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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
)
Enter fullscreen mode Exit fullscreen mode

addHours() Example

<?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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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
)
Enter fullscreen mode Exit fullscreen mode

  1. Read Also : Carbon Add Years To Date In Laravel
  2. Read Also : Carbon Add Months To Date In Laravel
  3. Read Also : Carbon Add Days To Date In Laravel

Thanks for the reading...!!
Don't forgot to like, share and comment.

Discussion (0)

pic
Editor guide