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

问题描述 投票:0回答:3

财政年度从4月开始。所以举个例子。

$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-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;

出放是FY-14-15 FY-16-17 。

我的问题是,缺少财政年度FY-15-16。此外,我已经尝试了一个更好的代码,以获得更多的年数说startdate ='2015-01-28' 和endDate ='2018-01-28'。

php strtotime
3个回答
1
投票

那这样呢?

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

0
投票

年之前不见了

<?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 
?> 
© www.soinside.com 2019 - 2024. All rights reserved.