Laravel 8 Carbon Add Minutes Example | How to Add Minutes in Laravel with Carbon Tutorial

Last updated on by Digamber

How to add minute, minutes and sub minutes in Laravel application using Laravel Carbon? Well, this tutorial will show you how to add minutes with Laravel Carbon in Laravel application.

Carbon is a PHP package developed by Brian Nesbit which prolongs PHP’s DateTime class. It offers some of the excellent functionality to handle with dates in PHP; It primarily deals with date timezones.

You will learn to add minutes on a current date with the help of Carbon package in Laravel 8, and this tutorial will demonstrate the Carbon in Laravel and specifically show you how to use addMinute(), addMinutes(), subMinutes() methods using Carbon date object.

Let’s begin understanding the Laravel Carbon example.

Getting Started

Laravel application can be installed with the following composer command:

composer create-project laravel/laravel --prefer-dist laravel-carbon-example

Move inside the project:

composer create-project laravel/laravel --prefer-dist laravel-carbon-example

Start laravel development server using below command:

php artisan serve

Open the app on below link:

http://127.0.0.1:8000

Thereafter, using below command generate a new controller for adding minute, minutes and sub minutes.

php artisan make:controller CalendarController

Define Minute with Laravel Carbon

Ideally, Inside the controller, use Carbon\Carbon afterwards within the controller define index method to evoke the view. The addMinute() method is configured using Carbon. Consequently, we are adding minutes, and you can see the output in the browser.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class CalendarController extends Controller
{

    // view
    public function index()
    {
        return view('welcome');
    }

    // add minute
    public function addMinute()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addMinute();
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

}

Define Minutes with Laravel Carbon

Likewise, you can pass minutes in addMinutes(7) method to add minutes using Carbon.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class CalendarController extends Controller
{

    // view
    public function index()
    {
        return view('welcome');
    }

    // add minutes
    public function addMinutes()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addMinutes(6);
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

}

Define Sub Minutes with Carbon

Same way you can use the subMinute() and subMinutes() methods.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class CalendarController extends Controller
{

    // view
    public function index()
    {
        return view('welcome');
    }

    // add sub minutes
    public function addSubMinute()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->subMinutes(7);
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }
}

Summary

The Carbon package powers the date-time in this tutorial you have seen how to easily add minutes and sub minutes using PHP’s very own Carbon package within the Laravel application.