11

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.

paxdiablo
889k243 gold badges1.6k silver badges2k bronze badges
asked Oct 1, 2015 at 5:34
0

8 Answers 8

19

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');
Vidz
6271 gold badge8 silver badges16 bronze badges
answered Oct 1, 2015 at 5:46
Sign up to request clarification or add additional context in comments.

Comments

5

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);
}
answered Oct 1, 2015 at 5:56

Comments

2

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);
answered Oct 1, 2015 at 5:48

Comments

2

Its too Simple

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

try this one!

answered Mar 8, 2016 at 8:06

Comments

1

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
answered Oct 14, 2019 at 6:32

Comments

1

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 
answered Dec 30, 2021 at 9:56

Comments

0

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
answered Apr 1, 2022 at 7:09

Comments

-2

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.

answered Oct 1, 2015 at 6:45

1 Comment

I don't think this is right. If my financial year starts in April (not July like the OP) then I'd swap 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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.