Throughout this tutorial, you will learn how to add an hour, add hours, add sub hour, and add sub hours in Laravel 9 application with the help of the PHP Carbon package.
This Laravel Carbon tutorial will clarify brevity, although this Laravel add hour example is built with Laravel 9; however, you can use the similar approach in the older versions like Laravel 7 | 6.
When it comes to handling date and time in PHP-based frameworks, then you can rely on Carbon module with your eyes closed on; it is powered by PHP and extends the PHP’S date-time class.
Ideally, when you develop an application, you often have to deal with date-time. Consequently, we will share with you how to add an hour, hours, sub hour, and sub hours on a current or existing date with the help of Carbon package in Laravel project.
Enough explanation, let’s get to the point.
Getting Started
If Laravel app is not installed then use the below command otherwise skip this step:
composer create-project laravel/laravel --prefer-dist laravel-carbon-tutorial
Get into the app:
composer create-project laravel/laravel --prefer-dist laravel-carbon-tutorial
Start the application:
php artisan serve
Open the app to see the results on the browser:
http://127.0.0.1:8000
Afterwards, you need a controller to add hour and sub hour in Laravel application:
php artisan make:controller DateTimeController
All the below code will go to app/Http/Controllers/DateTimeController.php file:
Define Hour with addHour()
This is the simplest example, in the below example we are adding hour and aligning in the controller by defining a addHours() function.
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateTimeController extends Controller
{
public function index()
{
return view('home');
}
public function addHour()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->addHour();
print(""
.print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
}
Define Hours with addHours()
Add hours and pass the number of hours in the method, all is happening with the help of Carbon class that we added on the top of the controller.
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateTimeController extends Controller
{
public function index()
{
return view('home');
}
public function addHours()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->addHours(6);
print(""
.print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
}
Define Sub Hour with subHour()
It is as simple as we added the hour but this time you have to call the subHour()
method of Carbon object.
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateTimeController extends Controller
{
public function index()
{
return view('home');
}
public function addSubHour()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->subHour();
print(""
.print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
}
Define Sub Hours with subHours()
Eventually, you can add sub hours by passing the hours values in subHours()
method:
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateTimeController extends Controller
{
public function index()
{
return view('home');
}
public function addSubHours()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->subHours(3);
print(""
.print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
}
Fret not; here is the final conjugated code:
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateTimeController extends Controller
{
public function index()
{
return view('home');
}
public function addHour()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->addHour();
print(""
.print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
public function addHours()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->addHours(6);
print("
".print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
public function addSubHour()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->subHour();
print("
".print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
public function addSubHours()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->subHours(3);
print("
".print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
}
Summary
Ultimately, we saw how to add hours and sub hours with the Carbon module in the Laravel application.
I believe this will help you to deal with date and time in the near future, i assume you would love the brevity of this tutorial.