Laravel Carbon Get Last Day of Month Tutorial


Share:
 | Published By: Admin | Published On: Sep 24, 2021 | Category: PHP Laravel 8 Laravel


Hi Artisan,

Today, In This example, I will share with you how to get the last day of the month in laravel using carbon in laravel so it can easy to use in laravel app.

So, I will tell something carbon provided inbuilt function endOfMonth so I will use this function and get last day of the month in laravel carbon.

Here, laravel carbon get last day of month, get last day of the month in laravel, get last day of month carbon, PHP carbon get last day of the month, laravel carbon endOfMonth, laravel carbon end of the month.

Let's see I will show you a full example of get the last day of month in laravel using carbon so follow mine below code.

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2: Create Controller
<?php

namespace App\Http\Controllers;

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

class HomeController extends Controller
{
    /**
     * laravel carbon get last day of the month...
     *
     * @return string
    */
    public function index()
    {
        $myDate = '01/01/2021';
        $date = Carbon::createFromFormat('m/d/Y', $myDate)
                        ->endOfMonth()
                        ->format('Y-m-d');
  
        var_dump($date);
    }

}
Step 3: Create Route
<?php

use App\Http\Controllers\HomeController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('last-day',[HomeController::class,'index']);
Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/last-day
Output
string(10) "2021-01-31"

It Will Help You..

Share: