财政年度从四月开始。例如:
$startDate='2015-01-28' // Start date
$endDate ='2018-05-28' // End date
现在输出我看起来就像这个FY-14-15(开始日期在2015年下半年之前),FY-15-16,FY-16-17,FY-17-18,FY-18-19 (结束日期在2018年apr之后)。这种格式我需要查询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',
答案 0 :(得分: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
答案 1 :(得分: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
?>