Carbon Add Days To Date In Laravel 9

Websolutionstuff | Nov-17-2022 | Categories : Laravel PHP

In this article, we will see carbon add day and add days to date in laravel 9. Carbon provides the addDay() and addDays() methods to add days to date objects. You can add a single day and also you can add days to the current date or any date. In the addDays() function need to pass the no. of days as a parameter.

So, let's see laravel 9 carbon add days to date, how to add a day to date in laravel 9, PHP carbon add a day.

Carbon addDay() Function

 In this example, we will add a day to the current date using the carbon addDay() function.

<?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);
    }
}

 

 Output:

Carbon\Carbon Object
(
    [date] => 2022-11-02 08:51:57.696013

    [timezone_type] => 2

    [timezone] => GMT
)

Carbon\Carbon Object
(
    [date] => 2022-11-03 08:51:57.696023

    [timezone_type] => 2

    [timezone] => GMT
)

 

 

Carbon addDays() Function

In this example, we will add 5 days to the current date using the carbon addDays() function.

<?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);
    }
}

 

Output:

Carbon\Carbon Object
(
    [date] => 2022-11-02 08:55:58.896013

    [timezone_type] => 2

    [timezone] => GMT
)

Carbon\Carbon Object
(
    [date] => 2022-11-07 08:55:59.796023

    [timezone_type] => 2

    [timezone] => GMT

 


You might also like:

Recommended Post
Featured Post
How To Get Current Date And Time In Node.js
How To Get Current Date And Ti...

In this example we will see how to get current date and time in Node.js application. In Node.js date and time are handle...

Read More

Sep-01-2021

Google Map With Draggable Marker Example
Google Map With Draggable Mark...

In this example I will show you how to add a google map to your website, In many websites you can see they have added ma...

Read More

Jun-11-2020

Boosting Angular 15 Performance: Tips & Tricks
Boosting Angular 15 Performanc...

In the world of web development, performance is a crucial factor that directly affects user experience. Angular 15, the...

Read More

Jun-09-2023

Laravel whereHas and orWhereHas Query Example
Laravel whereHas and orWhereHa...

In this article, we will see the laravel whereHas and orWhereHas query example. whereHas and orWhereHas query is us...

Read More

Jan-19-2021