【问题标题】:How do I echo the financial year?我如何回应财政年度?
【发布时间】:2022-04-01 15:09:10
【问题描述】:

我是 PHP 新手,我有一个关于如何回显财政年度的问题。

对于日历年的回声,我使用:

<?php echo date("y"); ?>

我的财政年度从上一个日历年的 7 月 1 日开始,到 6 月 30 日结束,我想回应这个财政年度。

我不知道该怎么做。我搜索了这个问题的答案,但找不到适合我的简单解决方案。

预期输出

如果是 2015 年 6 月的某个时间,我想打印 2015 作为年份,然后从下个月的第一天开始打印 2016

【问题讨论】:

    标签: php date calendar


    【解决方案1】:

    尝试类似:

    if ( date('m') > 6 ) {
        $year = date('Y') + 1;
    }
    else {
        $year = date('Y');
    }
    

    简写:

    $year = ( date('m') > 6) ? date('Y') + 1 : date('Y');
    

    【讨论】:

      【解决方案2】:

      试试这个:

      if (date('m') <= 6) {//Upto June 2014-2015
          $financial_year = (date('Y')-1) . '-' . date('Y');
      } else {//After June 2015-2016
          $financial_year = date('Y') . '-' . (date('Y') + 1);
      }
      

      【讨论】:

        【解决方案3】:

        这应该很简单,比如:

        if (date('m') <= 6) {
            $year = date('Y');
        } else {
            $year = date('Y') + 1;
        }
        

        或者,您可以使用单个表达式将月份映射到零/一个值,具体取决于它是在日历年的上半年还是下半年,然后将其添加到您的日历年:

        $year = date('Y') + (int)((date('m') - 1) / 6);
        

        【讨论】:

          【解决方案4】:

          太简单了

          if (date('m') >= 6) {
              $year = date('Y') + 1;
          } else {
              $year = date('Y');
          }
          

          试试这个!

          【讨论】:

            【解决方案5】:

            对于印度,财政年度从每年的四月开始。

            大部分采用 2019-20 财年格式

            为此使用以下代码:

            //for current date month  used *** date('m')
            
                $date=date_create("2019-10-19");
            
                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;
            
            // O/P : FY 2019-20
            

            【讨论】:

              【解决方案6】:

              返回财政年度的基于函数的输入日期参数​​,您也可以定义格式....

              function getFinancialYear($inputDate,$format="Y"){
                  $date=date_create($inputDate);
                  if (date_format($date,"m") >= 4) {//On or After April (FY is current year - next year)
                      $financial_year = (date_format($date,$format)) . '-' . (date_format($date,$format)+1);
                  } else {//On or Before March (FY is previous year - current year)
                      $financial_year = (date_format($date,$format)-1) . '-' . date_format($date,$format);
                  }
              
                  return $financial_year;
              }  
              

              使用示例:

              echo getFinancialYear("2021-12-13","Y");//2021-2022
              
              echo getFinancialYear("2021-12-13","y");//21-22 
              

              【讨论】:

                【解决方案7】:

                这是一些在 php 中获取财政年度的单行代码

                4 位格式

                echo date("m") >= 4 ? date("Y"). '-' . (date("Y")+1) : (date("Y") - 1). '-' . date("Y");
                //Will return 2022-2023 as per current year
                

                2 位格式

                echo date("m") >= 4 ? date("y"). '-' . (date("y")+1) : (date("Y") - 1). '-' . date("y");
                //Will return 22-23 as per current year
                

                【讨论】:

                  【解决方案8】:

                  您可以简单地调用:

                  echo date('Y', mktime(0, 0, 0, 6+date('m')));
                  

                  根据mktime的文档:

                  相对于上一年年末的月份数。值 1 到 12 参考相关年份的正常日历月。小于 1 的值(包括负值)以相反的顺序引用上一年中的月份,因此 0 是 12 月,-1 是 11 月,等等。大于 12 的值引用下一年中的适当月份。

                  将当前月份加 6 表示您的预期财政年度。

                  【讨论】:

                  • 我认为这是不对的。如果我的财政年度从 4 月开始(而不是像 OP 那样的 7 月),那么我会将6 换成3。假设我们目前在 2019 年 1 月,那么 $m = 1echo date('Y', mktime(0, 0, 0, 3+$m)); 输出 2019 时应该输出 2018 直到 2019 年 3 月结束。ideone.com/f010Lx
                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-06-02
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多