Laravel Carbon Get Last Month
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...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial