PHP calculate financial year

11,385

Solution 1

When performing calculations on dates it is a good idea to extend the DateTime class. This keeps all your date calculations encapsulated and in one place. Over time you will build up a very useful library.

To calculate the fiscal year you could extend DateTime thus:-

class MyDateTime extends DateTime
{
    /**
    * Calculates start and end date of fiscal year
    * @param DateTime $dateToCheck A date withn the year to check
    * @return array('start' => timestamp of start date ,'end' => timestamp of end date) 
    */
    public function fiscalYear()
    {
        $result = array();
        $start = new DateTime();
        $start->setTime(0, 0, 0);
        $end = new DateTime();
        $end->setTime(23, 59, 59);
        $year = $this->format('Y');
        $start->setDate($year, 4, 1);
        if($start <= $this){
            $end->setDate($year +1, 3, 31);
        } else {
            $start->setDate($year - 1, 4, 1);
            $end->setDate($year, 3, 31);
        }
        $result['start'] = $start->getTimestamp();
        $result['end'] = $end->getTimestamp();
        return $result;
    }
}

This will give a result you can easily include into your query (which you should really change to mysqli or pdo if you can).

You can use the new function like this:-

$mydate = new MyDateTime();    // will default to the current date time
$mydate->setDate(2011, 3, 31); //if you don't do this
$result = $mydate->fiscalYear();
var_dump(date(DATE_RFC3339, $result['start']));
var_dump(date(DATE_RFC3339, $result['end']));

If you wish you could modify the method to return the start and end dates as DateTime objects:-

$result['start'] = $start;
$result['end'] = $end;
return $result;

Which you can then format directly for inclusion in your query:-

$mydate = new MyDateTime();
$mydate->setDate(2011, 3, 31);
$result = $mydate->fiscalYear();
$start = $result['start']->format('Y M d');
$end = $result['end']->format('Y M d');

See the manual for date formats

Solution 2

/**
* Current financial year first date where financial year starts on 1st April
*/
$financialyeardate = 
(date('m')<'04') ? date('Y-04-01',strtotime('-1 year')) : date('Y-04-01');
Share:
11,385
Harinder
Author by

Harinder

Updated on June 06, 2022

Comments

  • Harinder
    Harinder about 2 years

    I want to calculate the financial year using php to get data from mysql tables.

    The requirement is to calculate student score in every financial year (31 march to 1 April). Is it possible to make any function that calculates these dates by itself every year?

    My table for students test score is storing test dates (2-sep-2012) and it has old record also for same student of (2-sep-2011) this year. I want out put of current year only. until now I can't get this, here is my code:-

    $result1 = mysql_query(
        "SELECT SUM(score), SUM(score_from) 
         FROM school_test_report, school_students 
         WHERE (school_test_report.student_id = school_students.student_id and
    school_test_report.class=school_students.class) 
         AND school_test_report.student_id='$id'
         AND school_test_report.subject = 'maths'
        /* something here to get dates  school_test_report.test_date is between 31 march to 1 April */"
    )
    or die(mysql_error());  
    $row = mysql_fetch_assoc($result1);
    echo $row['SUM(score)'].'/'. $row['SUM(score_from)'];
    

    Its giving me over all result not for one financial year.