Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I echo the financial year?

Tags:

date

php

calendar

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.

like image 423
sunny Avatar asked Oct 01 '15 05:10

sunny


6 Answers

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 
like image 107
Shantanu Sharma Avatar answered Nov 05 '22 19:11

Shantanu Sharma


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');
like image 28
Nishanth Matha Avatar answered Nov 05 '22 18:11

Nishanth Matha


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);
}
like image 37
Al Amin Chayan Avatar answered Nov 05 '22 17:11

Al Amin Chayan


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);
like image 22
paxdiablo Avatar answered Nov 05 '22 18:11

paxdiablo


Its too Simple

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

try this one!

like image 37
atifaltaf Avatar answered Nov 05 '22 17:11

atifaltaf


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
like image 35
Satish Avatar answered Nov 05 '22 17:11

Satish