Laravel Carbon Subtract Minutes Example

By Hardik Savani April 16, 2024 Category : Laravel

This article goes in detailed on laravel carbon subtract minutes. Here you will learn laravel carbon subtract minutes. This article will give you simple example of laravel carbon subtract 1 minute. i would like to show you laravel carbon subMinutes(). you will do the following things for subtract minutes to date in laravel.

You can subtract minutes on current date using carbon in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

If you need to sub minute or more minutes in date then you can use carbon in laravel. carbon provide subMinute() and subMinutes() method to sub minutes on carbon date object. so let's see some examples to subtract minute and minutes and sub minute and years from date.

Let's see example:

Example 1: Sub Minute

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Carbon\Carbon;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$currentDateTime = Carbon::now();

$newDateTime = Carbon::now()->subMinute();

print_r($currentDateTime);

print_r($newDateTime);

}

}

Output

Carbon\Carbon Object

(

[date] => 2020-11-05 04:32:50.651145

[timezone_type] => 3

[timezone] => UTC

)

Carbon\Carbon Object

(

[date] => 2020-11-05 04:31:50.651145

[timezone_type] => 3

[timezone] => UTC

)

Example 2: Sub Minutes

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Carbon\Carbon;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$currentDateTime = Carbon::now();

$newDateTime = Carbon::now()->subMinutes(2);

print_r($currentDateTime);

print_r($newDateTime);

}

}

Output

Carbon\Carbon Object

(

[date] => 2020-11-05 04:29:51.651667

[timezone_type] => 3

[timezone] => UTC

)

Carbon\Carbon Object

(

[date] => 2020-11-05 04:27:51.651667

[timezone_type] => 3

[timezone] => UTC

)

Example 3: Add Minute

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Carbon\Carbon;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$currentDateTime = Carbon::now();

$newDateTime = Carbon::now()->addMinute();

print_r($currentDateTime);

print_r($newDateTime);

}

}

Output

Carbon\Carbon Object

(

[date] => 2020-11-05 04:29:35.435461

[timezone_type] => 3

[timezone] => UTC

)

Carbon\Carbon Object

(

[date] => 2020-11-05 04:30:35.435461

[timezone_type] => 3

[timezone] => UTC

)

Example 4: Add Minutes

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Carbon\Carbon;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$currentDateTime = Carbon::now();

$newDateTime = Carbon::now()->addMinutes(5);

print_r($currentDateTime);

print_r($newDateTime);

}

}

Output

Carbon\Carbon Object

(

[date] => 2020-11-05 04:29:35.435461

[timezone_type] => 3

[timezone] => UTC

)

Carbon\Carbon Object

(

[date] => 2020-11-05 04:34:35.435461

[timezone_type] => 3

[timezone] => UTC

)

I hope it can help you...

Tags :
Shares