如何使用 php 以特定格式列出开始日期和结束日期之间的财政年度?

How to List financial years in a specific format between a start date and end date using php?

财政年度从四月开始。例如:

$startDate='2015-01-28' // Start date
$endDate ='2018-05-28'  // End date

现在我看到的输出是这样的 FY-14-15(开始日期早于 2015 年 4 月),FY-15-16,FY-16-17,FY-17-18,FY-18 -19(结束日期晚于 2018 年 4 月)。这种格式我需要查询 db(MySql) 以获取一些基于 FY-Year1-Year2 的值。 到目前为止我已经试过了..

$startDate='2015-01-28' // Start date
$endDate ='2017-05-28'
function calculateFinancialYears($startdate,$enddate){ // function calculates  FY years

        $d = date_parse_from_format("Y-m-d", $startdate);
        $y1 = $d['year'];
        if($d['month']<4){ //checking if startdate month falls before april 

            $fy1 = "FY-".($d['year']-2001)."-".(($d['year'])-2000);//concat FY- for desired Output

        }else {
             $fy1 = "FY-".(($d['year'])-2000)."-".($d['year']-1999);
        }
        echo $fy1;

        $d2 = date_parse_from_format("Y-m-d", $enddate);
        $y2 = $d2['year'];
        if($d2['month']<4){

            $fy2 = "FY-".($d2['year']-2001)."-".(($d2['year'])-2000);

        }else {
             $fy2 = "FY-".(($d2['year'])-2000)."-".($d2['year']-1999);
        }
        echo $fy2;
return $fy1; return $fy2;

输出 Put 是 FY-14-15 FY-16-17 .

我的问题是,缺少财政年度 FY-15-16。另外,我尝试过的并不是更好的代码来获得更多年数,比如 startdate ='2015-01-28' 和 endDate ='2018-01-28',

/ 在 year

之前丢失
<?php 
/* 
*    AUTHOR: http://www.tutorius.com/calculating-a-fiscal-year-in-php 
* 
*    This function figures out what fiscal year a specified date is in. 
*    $inputDate - the date you wish to find the fiscal year for. (12/4/08) 
*    $fyStartDate - the month and day your fiscal year starts. (7/1) 
*    $fyEndDate - the month and day your fiscal year ends. (6/30) 
*    $fy - returns the correct fiscal year 
*/ 
function calculateFiscalYearForDate($inputDate, $fyStart, $fyEnd){ 
    $date = strtotime($inputDate); 
    $inputyear = strftime('%Y',$date); 

    $fystartdate = strtotime("$fyStart/$inputyear"); 
    $fyenddate = strtotime("$fyEnd/$inputyear"); 

    if($date < $fyenddate){ 
        $fy = intval($inputyear); 
    }else{ 
        $fy = intval(intval($inputyear) + 1); 
    } 

    return $fy; 

} 

// my fiscal year starts on July,1 and ends on June 30, so... 
echo calculateFiscalYearForDate("5/15/08","7/1","6/30")."\n"; 
// returns 2008 

echo calculateFiscalYearForDate("12/1/08","7/1","6/30")."\n"; 
// returns 2009 
?> 

这个怎么样?

function calcFY($startDate,$endDate) {

    $prefix = 'FY-';

    $ts1 = strtotime($startDate);
    $ts2 = strtotime($endDate);

    $year1 = date('Y', $ts1);
    $year2 = date('Y', $ts2);

    $month1 = date('m', $ts1);
    $month2 = date('m', $ts2);

    //get months
    $diff = (($year2 - $year1) * 12) + ($month2 - $month1);

    /**
     * if end month is greater than april, consider the next FY
     * else dont consider the next FY
     */
    $total_years = ($month2 > 4)?ceil($diff/12):floor($diff/12);

    $fy = array();

    while($total_years >= 0) {

        $prevyear = $year1 - 1;

        //We dont need 20 of 20** (like 2014)
        $fy[] = $prefix.substr($prevyear,-2).'-'.substr($year1,-2);

        $year1 += 1;

        $total_years--;
    }
    /**
     * If start month is greater than or equal to april, 
     * remove the first element
     */
    if($month1 >= 4) {
        unset($fy[0]);
    }
    /* Concatenate the array with ',' */
    return implode(',',$fy);
}

输出:

echo calcFY('2015-03-28','2018-05-28');
//FY-14-15,FY-15-16,FY-16-17,FY-17-18,FY-18-19