Laravel Carbon Get Last Month

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
Laravel Carbon Get Last Month

Hello Dev’s,
Today now in this blog, I will share with you how to get last month by using carbon in laravel. It is easy and also very simple to get last month with carbon in laravel.

So now we can see here that it is last month in the Carbon\Carbon class. At First we will use carbon in now() to use current month and thenuse  subMonth() function to get last month.

Now at first example in numeric month output as like “11”

And then 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"

Read Also : Add Charts Using Highcharts In Laravel 8

I hope it will help you. Also you can follow us on Facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →