Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

get Financial Year Month and Year

I am trying to get all the Months for Financial Year using below Code

for ($i = 0; $i <= 11; $i++) {
               $months[] = date("M-Y", strtotime( date( 'Y-m-01' )." -$i months"));
            }

the Output i am getting is

0 => "Mar-2022"
1 => "Feb-2022"
2 => "Jan-2022"
3 => "Dec-2021"
4 => "Nov-2021"
5 => "Oct-2021"
6 => "Sep-2021"
7 => "Aug-2021"
8 => "Jul-2021"
9 => "Jun-2021"
10 => "May-2021"
11 => "Apr-2021"

Where i want it should start with April-2021 – May -2021———- Jan-2022, Feb-2022, March-2022

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can i get the same. I am new Leaner. Thanks in advance

>Solution :

You can try something like this:

$period = CarbonPeriod::create(
    now()->month(4)->subMonths(12)->startOfMonth()->format('Y-m-d'),
    '1 month',
    now()->month(3)->endOfMonth()->format('Y-m-d')
);
$finYear = [];
foreach ($period as $p) {
    $finYear[] = $p->format('M-Y');
}
dd($finYear);

// output

array:12 [
  0 => "Apr-2021"
  1 => "May-2021"
  2 => "Jun-2021"
  3 => "Jul-2021"
  4 => "Aug-2021"
  5 => "Sep-2021"
  6 => "Oct-2021"
  7 => "Nov-2021"
  8 => "Dec-2021"
  9 => "Jan-2022"
  10 => "Feb-2022"
  11 => "Mar-2022"
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading