phparraysajaxcodeigniterphppgadmin

financial year array between start date and end date php


I would like to do something of this sort in php (codeigniter), when the enter start date and end date, I would like to echo all financial years between those dates. Financial year starts from 01-04-2001 to 31-03-2002 (April to March)

Eg:

public function getfinancialyears(){
      
        $startdate = $this->input->post('Date1'); //13-06-2008 *data from user input in controller
        $enddate = $this->input->post('Date2'); //27-01-2020 *data from user input in controller

}

Required Output : fy = {2008-2009, 2009-2010, 2010-2011,......2019-2020};

*Want a logc that could show output of all financial years between these dates in array so that i can use fy array in loop to get further data accordingly.


Solution

  • You can use next simple code as solution:

    <?php
    function getfinancialyears($startdate, $enddate){
        $startYear = date('Y', strtotime($startdate));
        $endYear   = date('Y', strtotime($enddate));
    
        $financeYears = [];
        
        while($startYear < $endYear ) {
            array_push($financeYears, $startYear . "-" . (++$startYear));
        }
        
        return $financeYears;
    }
    

    You also can test this code at PHPize.online