Get First And Last Date Of Previous Month In Laravle

Laravel makes it very easy to get the date. These dates can be beneficial for showing analytics in the admin area, like visits in 7 days or 30 days visits.

How to show today’s date in the view file (blade)?

In the blade file, you want to show the date in year-month-date format

{{date('Y-m-d')}}

You can change the format and even show the time like {{date(‘d-m-Y H:i:s’)}}

If you want to show footer copyright text with the current year, then use this: {{date(‘Y’)}}

Y = to show the full, y = to show in short

How to show the first and last date of the previous month?

$first = Carbon::now()->startOfMonth()->subMonth()->toDateString();
$last = Carbon::now()->subMonth()->endOfMonth()->toDateString();

The final code will be like

use Carbon\Carbon;

public function index()
{
     $first = Carbon::now()->startOfMonth()->subMonth()->toDateString();
     $last = Carbon::now()->subMonth()->endOfMonth()->toDateString();
     return view('page.index', compact('first', 'last'));
}

Related Posts