HDTuto.com

How to Subtract Days in Date in Laravel?



This article will provide some of the most important example how to subtract days in date in laravel. i explained simply step by step laravel subtract days to date. i would like to show you subtract days to date php laravel. you will learn laravel carbon subtract days. Here, Creating a basic example of subtract days carbon laravel.

You can subtract days on current date using carbon in laravel 6, laravel 7 and laravel 8 version.

If you need to subtract day or more days in date then you can use carbon in laravel. carbon provide subDay() and subDays() method to add days on carbon date object. so let's see some examples to adding day and days and sub day and days from date.

Let's see example:

Example 1: Sub Day

<?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()->subDay();

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-04 04:32:50.651151

[timezone_type] => 3

[timezone] => UTC

)

Example 2: Sub Days

<?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()->subDays(5);

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-10-31 04:29:51.651673

[timezone_type] => 3

[timezone] => UTC

)

Example 3: Add Day

<?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()->addDay();

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-06 04:29:35.435474

[timezone_type] => 3

[timezone] => UTC

)

Example 4: Add Days

<?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()->addDays(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-10 04:29:35.435474

[timezone_type] => 3

[timezone] => UTC

)

I hope it can help you...




November 6, 2020 | Category : Other



Blog Search