I am new to PHP and I have a question on how to echo a financial year.
For echo of calendar year, I use:
<?php echo date("y"); ?>
My financial year starts on 1 July of the previous calendar year and concludes on 30 June, and I want to echo the financial year.
I have no any idea how I can do it. I searched for answers to this problem but I cannot find an easy solution for me.
Expected output
If it is sometime in June 2015, I want to print 2015 as the year, and it would then print 2016 starting on the first day of the following month.
8 Answers 8
try something like:
if ( date('m') > 6 ) {
$year = date('Y') + 1;
}
else {
$year = date('Y');
}
Short hand notation:
$year = ( date('m') > 6) ? date('Y') + 1 : date('Y');
Comments
Try this:
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);
}
Comments
That should be simple enough, something like:
if (date('m') <= 6) {
$year = date('Y');
} else {
$year = date('Y') + 1;
}
Alternatively, you could use an single expression that maps the month to a zero/one value depending on whether it's in the first or second half of the calendar year, then adds that to your calendar year:
$year = date('Y') + (int)((date('m') - 1) / 6);
Comments
Its too Simple
if (date('m') >= 6) {
$year = date('Y') + 1;
} else {
$year = date('Y');
}
try this one!
Comments
For India,financial year start from April of every Year.
Mostly in FY 2019-20 format
For that use following code:
//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
Comments
Function based input date parameter thats return financial year and you can define format also....
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;
}
use example :
echo getFinancialYear("2021年12月13日","Y");//2021-2022
echo getFinancialYear("2021年12月13日","y");//21-22
Comments
Here is some single liner code for getting financial year in php
4 Digit Format
echo date("m") >= 4 ? date("Y"). '-' . (date("Y")+1) : (date("Y") - 1). '-' . date("Y");
//Will return 2022-2023 as per current year
2 Digit Format
echo date("m") >= 4 ? date("y"). '-' . (date("y")+1) : (date("Y") - 1). '-' . date("y");
//Will return 22-23 as per current year
Comments
You can simply call:
echo date('Y', mktime(0, 0, 0, 6+date('m')));
As per the docs of 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).
Adding 6 to the current month shifts to your expected financial year.
1 Comment
6 for 3. Assuming we're currently in January 2019 then $m = 1 but echo date('Y', mktime(0, 0, 0, 3+$m)); outputs 2019 when it should output 2018 until the end of March 2019. ideone.com/f010Lx