Skip to content
Advertisement

getting previous 6 months getting issue

I am facing issue to get last 6 months in the basis of month number

JavaScript

issue is this the above code return Oct-21Sep-21Aug-21Jul-21Jun-21May-21 and required result should be Oct-20 Sep-20 Aug-20 Jul-20 Jun-20 May-20

Advertisement

Answer

One way to deal with your year issue is to compare $vBillMonthPrev with the current month, and if it is greater than that, supplying a year parameter to mktime which is for the previous year. Then you can make a DateTime object from that date, and subtract one month in a loop to generate values into your $m_array. Note that it is preferable to subtract a month at a time rather than making a new date variable, as this will correctly deal with the case where the month underflows into the previous year.

JavaScript

Output:

JavaScript

Demo on 3v4l.org

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