Skip to content
Advertisement

Is there a better way to get the 12 months base from the current month and year in laravel using carbon

I have a graph like the attach image below.

How should we fetch 12 months base from the current month year?

Here’s my implementation for the complete 12 month period with month and year filter. Ignore other variables, just look the filter for fetching months.

JavaScript

But this only get the complete 12 months of the current year. I want to get the 12 months prior to the last month of the current year. For example please see the attach image and observe months prior to the latest month data and in this case is July regardless of the year.

enter image description here

And this is my proposed algorithm and ongoing implementation. If you have much better ways to come up with please post your answer.

JavaScript

Advertisement

Answer

use ->subMonth() or subMonths(n)
You can subtract one month or the number of months from Carbon date.
For example

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement