Laravel Carbon Subtract Minutes Example Tutorial

Aug 16, 2021 . Admin



Hi All,

Today, In this example, I will share with you how to sub minutes in laravel using carbon in laravel so it can easy to use in laravel app.

So,if you needed to subtract one minute or more minutes you can use carbon in laravel. carbon provide inbuilt function subMinute()

and subMinutes() method to sub minutes on carbon date object. so let's see some examples to sub minute and minutes.

Here, I will give you a full example of how to sub minutes in laravel using carbon in laravel. so follow my example code and you can use your laravel application to copy that code.

Example : 1
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon sub-minute...
     *
     * @return string
    */
    public function index()
    {
        $currentDateTime = Carbon::now();
        $newDateTime = Carbon::now()->subMinute();

        // echo "<pre />";
        print_r($currentDateTime);
        print_r($newDateTime);
    }

}
Output
Carbon\Carbon Object

(
    [date] => 2021-08-16 04:47:08.166172

    [timezone_type] => 3

    [timezone] => UTC
)

Carbon\Carbon Object

(
    [date] => 2021-08-16 04:46:08.166174

    [timezone_type] => 3

    [timezone] => UTC
)
Example : 2
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon sub minutes...
     *
     * @return string
    */
    public function index()
    {
        $currentDateTime = Carbon::now();
        $newDateTime = Carbon::now()->subMinutes(2);

        // echo "<pre />";
        print_r($currentDateTime);
        print_r($newDateTime);
    }

}
Output
Carbon\Carbon Object

(
    [date] => 2021-08-16 04:48:11.967213

    [timezone_type] => 3

    [timezone] => UTC
)

Carbon\Carbon Object

(
    [date] => 2021-08-16 04:46:11.967216

    [timezone_type] => 3

    [timezone] => UTC
)

It Will Help You..

#PHP #Laravel 8 #Laravel