4

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.

vascowhite
  • 18,120
  • 9
  • 61
  • 77
Harinder
  • 1,257
  • 8
  • 27
  • 54
  • the "test_date"; what its data type? –  May 27 '12 at 08:25
  • 2-sep-2011 this format (varchar)( maintaining old database ) – Harinder May 27 '12 at 08:29
  • 1
    Can you include a simple schema of the relevant tables? – Kim Stacks May 27 '12 at 08:33
  • possible duplicate of [Php fiscal year report, trouble with selecting proper year](http://stackoverflow.com/questions/6605248/php-fiscal-year-report-trouble-with-selecting-proper-year) – vascowhite May 27 '12 at 09:33
  • 1
    Please stop writing new code with the ancient `mysql_*` functions. They are no longer maintained and community has begun the [deprecation process](http://goo.gl/KJveJ) . Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you cannot decide, [this article](http://goo.gl/3gqF9) will help to choose. If you care to learn, [here is a good PDO-related tutorial](http://goo.gl/vFWnC). – vascowhite May 27 '12 at 10:10
  • 1
    Shouldn't your fiscal year be between 1st April and 31st March? 31st March to 1st April is only 1 day. – vascowhite May 27 '12 at 10:57
  • @vascowhite Not just begun, the deprecation process is all but complete. The only supported version of PHP that still has mysql_* support is 5.6 and that has less than a year of support remaining. As of PHP 7 the mysql_* functions are entirely removed. – GordonM Mar 06 '18 at 09:53
  • @GordonM 'Just begun' was accurate when I wrote the comment in 2012 :) – vascowhite Mar 07 '18 at 20:37
  • @vascowhite Ah yes, looks like somebody decided to necromance this question by posting an answer on it and caused it to bubble up the question list. I didn't notice its original post-date. – GordonM Mar 08 '18 at 09:46

6 Answers6

4

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

vascowhite
  • 18,120
  • 9
  • 61
  • 77
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');
Jai
  • 151
  • 1
  • 7
1

This thread is quite old but I wanted to add my refinement to vascowhites answer above.

The following class will handle different fiscal years rather than just the north American default.

The output is a little more flexible as well

class FiscalYear extends DateTime {

    // the start and end of the fiscal year
    private $start;
    private $end;

    /**
     * 
     * Create a fiscal year object
     * 
     * @param type $time date you wish to determine the fiscal year in 'yyyy-mm-dd' format
     * @param type $fiscalstart optional fiscal year start month and day in 'mm-dd' format
     */
    public function __construct($time = null, $fiscalstart = '07-01') {
        parent::__construct($time,null);
        list($m, $d) = explode('-', $fiscalstart);
        $this->start = new DateTime();
        $this->start->setTime(0, 0, 0);
        $this->end = new DateTime();
        $this->end->setTime(23, 59, 59);
        $year = $this->format('Y');
        $this->start->setDate($year, $m, $d);
        $this->end = clone $this->start;
        if ($this->start <= $this) {
            $this->end->add(new DateInterval('P1Y'));
            $this->end->sub(new DateInterval('P1D'));
        } else {
            $this->start->sub(new DateInterval('P1Y'));
            $this->end->sub(new DateInterval('P1D'));
        }
    }

    /**
     * return the start of the fiscal year
     * @return type DateTime
     */
    public function Start() {
        return $this->start;
    }

    /**
     * return the end of the fiscal year
     * @return type DateTime
     */
    public function End() {
        return $this->end;
    }

}


echo 'Date: ';
$t1 = new FiscalYear('2015-07-02');
echo $t1->Format('Y-m-d');
echo '<br />Start: ';
echo $t1->Start()->Format('Y-m-d');
echo '<br />End: ';
echo $t1->End()->Format('Y-m-d');

echo '<br /><br />Date: ';
$t2 = new FiscalYear('2015-06-29');
echo $t2->Format('Y-m-d');
echo '<br />Start: ';
echo $t2->Start()->Format('Y-m-d');
echo '<br />End: ';
echo $t2->End()->Format('Y-m-d');

echo '<br /><br />Date: ';
$t3 = new FiscalYear('2015-07-02','04-01');
echo $t1->Format('Y-m-d');
echo '<br />Start: ';
echo $t3->Start()->Format('Y-m-d');
echo '<br />End: ';
echo $t3->End()->Format('Y-m-d');

echo '<br /><br />Date: ';
$t4 = new FiscalYear('2015-06-29','04-01');
echo $t4->Format('Y-m-d');
echo '<br />Start: ';
echo $t4->Start()->Format('Y-m-d');
echo '<br />End: ';
echo $t4->End()->Format('Y-m-d');
DeveloperChris
  • 3,412
  • 2
  • 24
  • 39
0

another:

SELECT UNIX_TIMESTAMP(school_test_report.test_date) AS unixdate, ...
...
AND unixdate>=UNIX_TIMESTAMP('31-mar-<?php echo date("Y")-1; ?>') AND
AND unixdate<=UNIX_TIMESTAMP('1-apr-<?php echo date("Y"); ?>')

i dont know what else.

  • for 1st example will it calculate date between 31 march to 1 April every year ?? yes 2nd example will work 31-mar-2011 to 1-apr-2012 but i have to change dates every year is there any thing which will take these dates by it self ?? – Harinder May 27 '12 at 08:51
  • @vlzvl: as OP said - there is a varchar `2-sep-2011` there, so it won't work – zerkms May 27 '12 at 09:00
  • yaaa i tried that , but problem is this will come as 31-mar-2012 to 1-apr-2012 and if we make Y-1 it will not work in April bcos date will be 31-mar-2010 ... i want date between 31-mar-2011 to 1 apr-2012 ( – Harinder May 27 '12 at 09:01
  • yaa not working as its taking Y of same year `31-mar-2012 to 01-apr-2012` i want `31-mar-2011 to 01-apr-2012` – Harinder May 27 '12 at 09:23
  • check post again :) you can give date("Y") to mess with year if needed (date("Y")-1 will give you previous year) –  May 27 '12 at 09:31
  • i know but on 1-april 2011 it will give 31-mar-2010 with Y-1 and 1-jan-2012 it will be 31-mar-2011 so it will not work – Harinder May 27 '12 at 09:34
  • @vlzvl: `unix_timestamp` accepts valid dates as a parameter. `31-mar-2011` is not a valid date. Stop guessing ) – zerkms May 27 '12 at 10:01
0

It is Easy

$financial_year_to = (date('m') > 3) ? date('y') +1 : date('y');
$financial_year_from = $financial_year_to - 1;

$year= $financial_year_from .'-'.$financial_year_to;
Jay Momaya
  • 1,831
  • 19
  • 32
0
<?php
if(date('m')=="04"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="05"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="06"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="07"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="08"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="09"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="10"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="11"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="12"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="01"){
    $fyear = (date('Y')-1).'-'.(date('y'));
}
 if(date('m')=="02"){
    $fyear = (date('Y')-1).'-'.(date('y'));
}
 if(date('m')=="03"){
    $fyear = (date('Y')-1).'-'.(date('y'));
}

?>

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. [link-only-answers](https://meta.stackoverflow.com/tags/link-only-answers/info) – Sfili_81 Jun 14 '21 at 15:13