Carbon Add Minutes To Date In Laravel 9

Websolutionstuff | Nov-23-2022 | Categories : Laravel PHP

In this article, we'll explore how to add minutes to a date in Laravel 8, Laravel 9 and Laravel 10 using Carbon. Carbon simplifies the process with two functions: addMinute() and addMinutes(). These functions allow us to increment the minutes in a date and time.

To use them, we specify the number of minutes we want to add. It's a straightforward way to manipulate date and time in our Laravel 8, Laravel 9 and Laravel 10 applications.

Let's dive in and learn how to work with time in Laravel 8/9/10 more efficiently.

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
)

 

 

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
)

 

 

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

 

Conclusion:

In conclusion, we've expanded our knowledge of date and time manipulation in Laravel 8, Laravel 9 and Laravel 10 using Carbon. With the help of Carbon's addMinute() and addMinutes() functions, we can easily add minutes to a date and time, making our applications more dynamic and responsive.

 


You might also like:

Recommended Post
Featured Post
Laravel Accessor and Mutator Example
Laravel Accessor and Mutator E...

In this article, we will see the laravel accessor and mutator example. Here, we will learn what is accessor an...

Read More

Mar-16-2021

How To Create Dynamic Pie Chart In Laravel 8
How To Create Dynamic Pie Char...

In this article, we will see how to create a dynamic pie chart in laravel 8. Pie charts are used to represent...

Read More

Oct-02-2020

How to Format Number with 2 Decimal in PHP
How to Format Number with 2 De...

Hey there! If you've ever needed to work with numbers in PHP, you probably know how important it is to format them p...

Read More

Feb-26-2024

How To Add Foreign Key In Laravel 10 Migration
How To Add Foreign Key In Lara...

In this article, we will see how to add a foreign key in laravel 10 migration. Here, we will learn about laravel 10...

Read More

May-05-2023