Carbon Add Minutes To Date In Laravel 9

    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.

Carbon addMinute() Function

    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
)

Read Also: Carbon Add Hours To Date In Laravel 9
Carbon addMinutes() Function

    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
)

Read Also: Carbon Add Years To Date In Laravel 9
Carbon diffForHumans with addMinute() Function

    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:

2022-11-03 09:52:38
1 minute before
Carbon diffForHumans with addHours() Function

    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:

2022-11-03 09:54:47
15 minutes before

    You might also like:

Bình luận
Vui lòng đăng nhập để bình luận
Một số bài viết liên quan