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.
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
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');
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);
}
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);
Its too Simple
if (date('m') >= 6) {
$year = date('Y') + 1;
} else {
$year = date('Y');
}
try this one!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With