In this article, we will see carbon add minutes to date in laravel 9. Carbon provides the addMinute() and addMinutes() functions to add a minute to date object. addMinute() function may help to add minute to the given date and time. addMinutes() function may help to add minutes to a given date and time and also need to pass the no. of minutes as a parameter in the addMinutes() function.
So, let's see how to add minute in laravel 9, PHP carbon add minute, laravel 9 carbon add minutes to date.
In this example, we will add a minute to the current date and time object using the carbon addMinute() 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()->addMinute();
print_r($currentDateTime);
print_r($newDateTime);
}
}
Output:
Carbon\Carbon Object
(
[date] => 2022-11-03 09:45:30
[timezone_type] => 2
[timezone] => GMT
)
Carbon\Carbon Object
(
[date] => 2022-11-03 09:46:30
[timezone_type] => 2
[timezone] => GMT
)
In this example, we will add 10 minutes to current date and time using carbon addMinutes() 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()->addMinutes(10);
print_r($currentDateTime);
print_r($newDateTime);
}
}
Output:
Carbon\Carbon Object
(
[date] => 2022-11-03 09:48:59
[timezone_type] => 2
[timezone] => GMT
)
Carbon\Carbon Object
(
[date] => 2022-11-03 09:58:59
[timezone_type] => 2
[timezone] => GMT
)
In this example, we will use the diffForHumans() function with addMinute() to the current date and make it human-readable hours.
<?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()->addMinute());
print_r($currentDateTime);
print_r($newDateTime);
}
}
Output:
1 minute before
In this example, we will add 15 minutes 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()->addMinutes(15));
print_r($currentDateTime);
print_r($newDateTime);
}
}
Output:
15 minutes before
You might also like:
- Read Also: Laravel 9 whereIn / whereNotIn / orWhereIn / orWhereNotIn
- Read Also: How To Disable Dates After Week In jQuery Datepicker
- Read Also: Laravel 9 whereColumn Query Example
- Read Also: Bootstrap Daterangepicker Example