按给定月份获取财政年度

时间:2020-01-13 11:09:06

标签: php

财政年度从4月1日开始,而我们在1月,所以1月进入2020年,2月和3月也是如此。因此,如果我现在使用April,它应该属于2019年。目前,我使用以下代码,但是它不能按照我想要的方式工作

    $month =01;
   if ( $month >= 4 ) {
     $year = date('Y');
   }
   else {
    $year = date('Y')-1;
   }
   echo $year;

因此,当我将月份设置为1月(即一月)时,年份显示为2019,但实际上应该是2020。

反正有没有用PHP设置年份,所以当月份从4月开始到3月底结束时,那么当我输入月份时,我将获得正确的年份?

7 个答案:

答案 0 :(得分:1)

尝试类似这样的方法:Carbon::now()->subMonths(3)->year

这样,一月,二月和三月的日期将返回2019年,而四月将开始返回2020年。

答案 1 :(得分:0)

我希望这能解决您的问题

$date=date_create("2020-05-13");

echo "<br> Month: ".date_format($date,"m");
if (date_format($date,"m") >= 4) {//On or After April (FY is current year - next year)
    $financial_year = (date_format($date,"Y")) . '-' . (date_format($date,"y")+1);
} else {//On or Before March (FY is previous year - current year)
    $financial_year = (date_format($date,"Y")-1) . '-' . date_format($date,"y");
}

echo "<br> FY ".$financial_year;

答案 2 :(得分:0)

我认为我设法解决了这个问题。欢迎任何评论

$month =01;
$current_month = date('m');
if ($current_month >= '01' && $current_month < '4'){

   if ($month >= '01' && $month < '04'){
      $year = date('Y');
   } 

   if ($month >= 4){
      $year = date('Y')-1;
   }
} 

if ($current_month >= 4){
   if ($month >= 4){
      $year = date('Y');
   }

   if ($month < 4){
      $year = date('Y')+1;
   }
}

echo $year;

答案 3 :(得分:0)

大致情况:

$now = strtotime('now');
$firstApril = strtotime('1st april');
$year = ($now < $firstApril) ? date('Y')-1 : $date('Y');
return $year;

这将比较时间戳,如果时间戳小于今年4月1日,则将比较时间戳。

答案 4 :(得分:0)

您可以尝试以下操作:

 $month =01;
 if($month>=4&&$month<=12){
     $year=date('Y')-1;
 }
 if($month>=1&&$month<=3){
     $year=date('Y');
 }
   echo $year;

答案 5 :(得分:0)

这不是直接的答案,但是,如果您处理一个会计年度的多个财务/交易数据,则可能与几年前的情况相似。

可以随时检查此库-> https://github.com/RoussKS/financial-year

单次检查(尽管相当轻巧)可能会过大,但是如果您有更多要求,它会很有用。

需要财政年度的开始日期和财政年度的类型(在您的情况下,根据图书馆的规定为calendar),并提供结束日期,期间范围等。

根据使用的方法返回DateTimeImmutable对象或DatePeriod(仅获取business类型的财务年度的日期,期间或星期)

在您的情况下,用例将是

require_once __DIR__ . '/vendor/autoload.php';

// DateTimeAdapter
// If instantiating with string, it must be of ISO-8601 format 'YYYY-MM-DD'
$startDate = new \DateTime('2019-04-01');

$fy = new \RoussKS\FinancialYear\DateTimeAdapter('calendar', $startDate);

// January is the 10th period for a financial year based on 12 periods starting at 2019-04-01.
// First date of the period would give you  2020-01-01
echo $fy->getFirstDateOfPeriodById(10)->format('Y'); // 2020

随时进行测试并提供建议

答案 6 :(得分:0)

以下是获取当前会计年度的开始日期(4月1日开始)的方法。

public function getFinancialYear(){

    $today = Carbon::today();
    $month = $today->format('m');
    $year = $today->format('Y');

    $firstApril = 'first day of April '.$year;

    $financialYear = new Carbon($firstApril);

    if($month < 4){
        $financialYear->subYear();
    }

    return $financialYear;
    
}
相关问题