DEV Community 👩‍💻👨‍💻

Techsolutionstuff
Techsolutionstuff

Posted on • Originally published at techsolutionstuff.com

Carbon Add Days To Date In Laravel

In this tutorial, we will see carbon add days to date in laravel. Carbon provides addDay() and addDays() method to add days on carbon date object. So, we will see laravel carbon add days or how to add days in date in laravel 8. Using the carbon add() and sub() function you can change on date and time.

So, let's see an example of laravel 8 carbon add days to date or add days using carbon in laravel 8.
addDay() Example

Laravel add days to date provides you to add extra date using carbon.

<?php

namespace App\Http\Controllers;

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

class DateController extends Controller
{
    public function index()
    {
        $currentDateTime = Carbon::now();
        $newDateTime = Carbon::now()->addDay();

        print_r($currentDateTime);
        print_r($newDateTime);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output :

Carbon\Carbon Object
(
    [date] => 2022-01-16 06:36:25

    [timezone_type] => 2

    [timezone] => GMT
)

Carbon\Carbon Object
(
    [date] => 2020-01-17 06:36:25

    [timezone_type] => 2

    [timezone] => GMT
)
Enter fullscreen mode Exit fullscreen mode

Read Also : Laravel 8 Socialite Login with Google Account


addDays() Example

Using addDays() you can add days as per your requirements like the below example.

<?php

namespace App\Http\Controllers;

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

class DateController extends Controller
{
    public function index()
    {
        $currentDateTime = Carbon::now();
        $newDateTime = Carbon::now()->addDays(5);

        print_r($currentDateTime);
        print_r($newDateTime);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output :

Carbon\Carbon Object
(
    [date] => 2020-01-16 06:38:41

    [timezone_type] => 2

    [timezone] => GMT
)

Carbon\Carbon Object
(
    [date] => 2020-01-21 06:38:41

    [timezone_type] => 2

    [timezone] => GMT
) 
Enter fullscreen mode Exit fullscreen mode

You might also like :

Top comments (0)

19 Valuable Github Repositories for Beginners

>> Check out this classic DEV post <<