Laravel 9 Using Carbon Adjust Minutes Example

Sovary July 28, 2022 390
1 minute read

Today I will give you examples how to use Carbon package to add minutes and do adjustment minutes over the datetime. There are many functions Carbon is a package which is excellent functionality to adjust with dates in PHP.

You are going to learn how to add minutes on current date with Carbon package in Laravel, by this demonstrate you can use adjust minutes with addMinute(), addMinutes(), subMinute(), subMinutes() functions using Carbon date object. Let's take a look at examples below.

addMinute() -  used to add a minute on a date. For example below I created a controller and write print out to test.

Example #1

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DateController extends Controller
{
    public function index()
    {
        $current = Carbon::now();
        $new = Carbon::now()->addMinute();
        
        echo $current."<br>";
        echo $new;
    }
}

Output #1

2022-07-28 13:38:06
2022-07-28 13:39:06

addMinutes() - you can see it is plural which mean you can add any minutes on the date. The below example I will add 5 mintues on the current date.

Example #1

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DateController extends Controller
{
    public function index()
    {
        $current = Carbon::now();
        $new = Carbon::now()->addMinutes(5);
        
        echo $current."<br>";
        echo $new;
    }
}

Output #1

2022-07-28 13:41:59
2022-07-28 13:46:59

subMinute() -  used to substract a minute on a date.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DateController extends Controller
{
    public function index()
    {
        $current = Carbon::now();
        $new = Carbon::now()->subMinute();
        
        echo $current."<br>";
        echo $new;
    }
}

Output #1

2022-07-28 13:43:13
2022-07-28 13:42:13

subMinutes() - used to substract any minutes on the date. The below example I will minus 5 mintues on the current date.

Example #1

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DateController extends Controller
{
    public function index()
    {
        $current = Carbon::now();
        $new = Carbon::now()->subMinutes(5);
        
        echo $current."<br>";
        echo $new;
    }
}

Output #1

2022-07-28 13:46:04
2022-07-28 13:41:04

Hope this short article help you. Have a nice day!

You might also like:
 

 

Laravel  PHP  Laravel 9  Carbon Laravel 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him