Laravel Carbon Get Last Month

10-Apr-2023

.

Admin

Hi Guys,

In this blog, I will show you how to get last month using carbon in laravel. It is easy and simply to get last month with carbon in laravel.

We can see here that it is last month in the Carbon\Carbon class. First you can use carbon in now() to use current month and subMonth() function is used to get last month.

First example in numeric month output like "11"


and second example in month name in string for example "November".

Example 1 :

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function getLastMonth()

{

$date = \Carbon\Carbon::now();

$lastMonth = $date->subMonth()->format('m'); // 11

dd($lastMonth);

}

Output :

"11"

Example 2 :

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function getLastMonth()

{

$date = \Carbon\Carbon::now();

$lastMonth = $date->subMonth()->format('F'); // November

dd($lastMonth);

}

Output :

"November"

It will help you...

#Laravel 7

#Laravel

#PHP

#Laravel 6