vicky
vicky

Reputation: 31

Increment within Tax year

I wanted to increment the months within the financial year (April to March)and the following piece of code which I am currently using can increment the months within a mentioned year. How could I calculate the values for a financial year. I am new to PHP and please help me on this.

if (isset($_REQUEST['Receipts'])) {
$months = array( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
$year=2009;
foreach ($months as $i=>$month) {
$monthNumber=$i+1;
$numberOfDaysInMonth=cal_days_in_month(CAL_GREGORIAN, $monthNumber, $year);
$params['Date'] = "$numberOfDaysInMonth $month $year";
}

The output currently I get are the values for the year 2009 ( Jan 2009, Feb 09, Mar 09, Apr 09, May 09, Jun 09, jul 09, Aug 09, Sep 09, Oct 09, Nov 09, Dec 09) but I wanted for a financial year like Apr 2009, May 2009,......to..........Jan 2010, Feb 2010, Mar 2010

Upvotes: 1

Views: 109

Answers (1)

Clément Malet
Clément Malet

Reputation: 5090

Re-order your month array to start with Apr, and increment the year once Dec has been treated :

$months = array( "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar");
$year=2009;
foreach ($months as $i=>$month) {
    $monthNumber=$i+1;
    $numberOfDaysInMonth=cal_days_in_month(CAL_GREGORIAN, $monthNumber, $year);
    $params['Date'] = "$numberOfDaysInMonth $month $year";
    if ($month == 'Dec') {
        ++$year;
    }
}

You are rewriting on $params['Date'] everytime by the way, not sure that's what you want

Upvotes: 1

Related Questions