Home > OS >  get Financial Year Month and Year
get Financial Year Month and Year

Time:03-18

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

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

CodePudding user response:

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"
]
  • Related