In this tutorial, we will see example of carbon add hours in laravel, here I will give you a simple example of laravel carbon add hours, carbon provides many function like addHour(), addHours() to add hour in laravel 8. Using the carbon addHour() or addHours() function you can change the hours in the date in laravel 8.
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.
So, let's see an example of laravel 8 carbon add hours to date or add hours using carbon in laravel 8.
The carbon addHour() function is used to add the hour on a date. So, here I will give you an example of the current date to add an hour.
<?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 :
Current Date and Time : 2022-01-16 08:39:51
Current Date with Add Hour : 2022-01-16 09:39:51
The Carbon addHours() function is used to add hours as per your requirements on the date. So, here I will give you an example of the current date to add 6 hours.
<?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(6);
print_r($currentDateTime);
print_r($newDateTime);
}
}
Output :
Current Date and Time : 2022-01-16 08:40:34
Current Date with Add Hours : 2022-01-16 15:40:34
You might also like :