【问题标题】:Get financial year by given month按给定月份获取财政年度
【发布时间】:2020-01-13 11:09:06
【问题描述】:

财政年度从 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 即 1 月时,年份显示为 2019 年,但实际上应该是 2020 年。

有没有办法在 PHP 中设置年份,所以当月份从 4 月开始到 3 月底结束,所以当我输入一个月时,我会得到正确的年份?

【问题讨论】:

  • 你说所以如果我现在使用四月,它应该落在2019年。因此,这意味着 2019 财年将于 4 月 1 日结束。是吗?
  • 是的。每年的财政年度将从 4 月 1 日开始。
  • 你也说所以当我将月份设置为1 即1 月时,年份显示为2019 但实际上应该是2020 怎么会?财政年度仍将在 4 月 1 日结束。那么为什么 1 月会显示为 2020 年呢?
  • 您的代码是正确的,但您的解释不正确,并且与您想要实现的目标相矛盾。一月,二月,三月和最后四月的输出应该是什么?
  • 因为如果我想从 mysql 中提取收入或支出等数据,应该是 2020 年。我想我也找到了答案。请检查我将发布的答案

标签: php


【解决方案1】:

试试这样的:Carbon::now()->subMonths(3)->year

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

【讨论】:

    【解决方案2】:

    我想我设法解决了这个问题。欢迎任何cmets

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

    【讨论】:

    • 那么对于May 月,它将是 2019 年?
    • 看看@vivek_23
    【解决方案3】:

    希望能解决你的问题

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

    【讨论】:

      【解决方案4】:

      类似的东西:

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

      这将比较时间戳,如果它小于今年的 4 月 1 日,那么它将与上一年相比。

      【讨论】:

        【解决方案5】:

        你可以试试这个:

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

        【讨论】:

          【解决方案6】:

          这不是一个直接的答案,但如果您在一个财政年度为您的企业处理多个财务/交易数据,您可能处于与几年前类似的位置。

          请随意查看这个库 -> 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
          

          随时测试和建议

          【讨论】:

            【解决方案7】:

            以下是获取当前财政年度开始日期(开始日期为 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;
                
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-07-01
              • 2020-07-20
              • 2014-04-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多