Laravel 9 Using Carbon Adjust Minutes Example

Sovary July 28, 2022 551
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

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him