开发者

Php fiscal year report, trouble with selecting proper year

I am building a report for a client using开发者_如何学JAVA php and codeigniter.

This report is a financial report, as such, I must reflect money collected since the beginning of the fiscal year. In this case, July 31st.

I already query the user for the date of the report, but how would I get php to know which fiscal year to take?

I have a rough idea, something along the lines of

'If Month-Day is before July 31, use current year-1 Else use current year'

But I do not know how I would code that, or if it would work, or if there is a more elegant way of doing the same thing.


I think your best bet is the dateTime class.
http://us3.php.net/manual/en/class.datetime.php
I think something like this is what you will need

public function getCurrentYear(DateTime $dateToCheck)
{
    $today = new DateTime();
    $currentYear = (int)$today->format('Y');
    $endFiscalYear = new DateTime('31 July'); //year left out, so will default to this year
    if($dateToCheck < $endFiscalYear){ //you need PHP >= 5.2.2 for this to work
        $currentYear--;
    }
    return $currentYear;
}

You can set $today by doing something like :-

$today = new DateTime('20 June 2011');

Read more in the link above

Here is a slightly different version which should be a bit more robust as it will return the fiscal year of any date you give it, not just dates in the current year.

function getFiscalYear(DateTime $dateToCheck)
{
    $fiscalYearEnd = '31 July';
    $year = (int)$dateToCheck->format('Y');
    $fiscalyearEndDate = new DateTime($fiscalYearEnd . ' ' . $year);
    if($dateToCheck <= $fiscalyearEndDate){
        $year--;
    }
    return $year;
}

use it like this :-

$dateToCheck = new DateTime('1 jan 2009'); // for example
$fiscalYear = getFiscalYear($dateToCheck);

This will return 2008

This version should work if your PHP version is < 5.2

function getFiscalYear($timestamp)
{
    $year = (int)date('Y', $timestamp);
    $fiscalYearEndDate = strtotime('31 July ' . $year);
    if($timestamp < $fiscalYearEndDate) $year--;
    return $year;
}

Use like this:-

$date = strtotime('1 Jan 2009');
fiscalYear = getFiscalYear($date);

Will return 2008


function get_fiscal_year($format = 'Y-m-d', $cutoff_date = '31 July') {
  $fiscal_year = strtotime($cutoff_date);
  if(time() < $fiscal_year)
    $fiscal_year = strtotime($cutoff_date .' last year');

  return date($format, $fiscal_year);

}
$fisical_year = get_fiscal_year();
$now = date('Y-m-d');

You then could use the function to query database with statement condition

transcation_date between $fisical_year and $now


If user gives the date: @givenDate and you want to search in your dateField (which is of type date), you can use something like:

WHERE dateField BETWEEN 
          DATE( CONCAT(   YEAR(@givenDate)-(MONTH(@givenDate)<8), '-08-01' ))
      AND DATE( CONCAT( 1+YEAR(@givenDate)-(MONTH(@givenDate)<8), '-07-31' ))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜