我如何回应财政年度?

How do I echo the financial year?

我是 PHP 的新手,我对如何回显财政年度有疑问。

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

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

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

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

预期输出

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

试试这样的东西:

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

速记符号:

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

这应该很简单,比如:

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

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

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

试试这个:

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);
}

您只需致电:

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

根据 mktime 的文档:

The number of the month relative to the end of the previous year. Values 1 to 12 reference the normal calendar months of the year in question. Values less than 1 (including negative values) reference the months in the previous year in reverse order, so 0 is December, -1 is November, etc. Values greater than 12 reference the appropriate month in the following year(s).

将当前月份加 6 会转移到您的预期财政年度。

太简单了

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

试试这个!

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

主要采用 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

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

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 

这里有一些用于获取 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