Level 50
July has 31 days, June has 30. Calling subMonths(1) on July 31st results in June 31st, which rolls over to July 1st.
You can use the first day of the month before adding or subtracting months:
// 2024-06-01
$first = Carbon::now()->firstOfMonth()->subMonths(1)->format('Y-m-d');
// 2024-06-30
$last = Carbon::now()->firstOfMonth()->subMonths(1)->lastOfMonth()->format('Y-m-d');
// Or just get the last day with format():
$last = Carbon::now()->firstOfMonth()->subMonths(1)->format('Y-m-t');
1 like