Laravel Carbon Subtract Days to Date Example


Share:
Published By: Admin, Published On: Aug 06, 2021, Category: Laravel 8 Laravel


Hi Dev,

Today,In this Article i will share something new how to subtract days to date in laravel using carbon in laravel so it can easy to use in laravel app.

Whenever,if you require to subtract day or more days in date then you can use carbon in laravel. carbon provide inbuilt function subDay() and subDays() method to add days on carbon date object. so let's see some examples to subday day and days from date.

Here, I will give you full example for how to subtract days to date in laravel using carbon. so follow my example code and you can use your laravel application copy that code.

Example : 1
<?php

namespace App\Http\Controllers;

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

class HomeController extends Controller
{
    /**
     * laravel carbon add days to date..
     *
     * @return string
    */
    public function index(Request $request)
    {
        $currentDateTime = Carbon::now();
        $newDateTime = Carbon::now()->subDay();

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

}
Output
Carbon\Carbon Object

(
    [date] => 2021-08-06 04:42:45.911452

    [timezone_type] => 3

    [timezone] => UTC
)

Carbon\Carbon Object

(
    [date] => 2021-08-05 04:42:45.911463

    [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 add days to date..
     *
     * @return string
    */
    public function index(Request $request)
    {
        $currentDateTime = Carbon::now();
        $newDateTime = Carbon::now()->subDays(10);

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

}
Output
Carbon\Carbon Object

(
    [date] => 2021-08-06 04:44:34.969575

    [timezone_type] => 3

    [timezone] => UTC
)

Carbon\Carbon Object

(
    [date] => 2021-07-27 04:44:34.969585

    [timezone_type] => 3

    [timezone] => UTC
)

I Hope It Will Help You..

Share: