Php财年报告,麻烦选择适当的年份 [英] Php fiscal year report, trouble with selecting proper year
问题描述
我正在使用php和codeigniter为客户创建报告。
此报告是一份财务报告,因此,我必须反映自财政年度。在这种情况下,7月31日。
我已经向用户查询报告的日期,但我如何让php知道要采取哪个会计年度? p>
我有一个粗略的想法,沿着
行的东西'如果月 - 日是在7月31日之前,使用current year-1
否则使用当前年度
但我不知道我将如何编码,或者如果它会工作,更优雅的做同样的事情。
我认为你最好的打赌是dateTime类。
http://us3.php.net/manual/en/class.datetime。 php
我认为这样的东西是你需要的
public function getCurrentYear DateTime $ dateToCheck)
{
$ today = new DateTime();
$ currentYear =(int)$ today-> format('Y');
$ endFiscalYear = new DateTime('31 July'); // year left out,所以默认为今年
if($ dateToCheck< $ endFiscalYear){//你需要PHP> = 5.2.2这个工作
$ currentYear--
}
return $ currentYear;
}
您可以通过执行以下操作设置$ today: -
$ today = new DateTime('20 June 2011');
在上面的链接中阅读更多
这是一个稍微不同的版本,应该是一个更健壮,因为它将返回任何日期的财政年度,而不只是当年的日期。
function getFiscalYear(DateTime $ dateToCheck)
{
$ fiscalYearEnd = '31 July';
$ year =(int)$ dateToCheck-> format('Y');
$ fiscalyearEndDate = new DateTime($ fiscalYearEnd。''。$ year);
if($ dateToCheck< = $ fiscalyearEndDate){
$ year--;
}
return $ year;
}
使用: -
$ dateToCheck = new DateTime('1 jan 2009'); //例如
$ fiscalYear = getFiscalYear($ dateToCheck);
这将返回2008
应该工作,如果你的PHP版本< 5.2
函数getFiscalYear($ timestamp)
{
$ year = ',$ timestamp);
$ fiscalYearEndDate = strtotime('31 July'。$ year);
if($ timestamp< $ fiscalYearEndDate)$ year--;
return $ year;
}
使用这样: -
$ b b
$ date = strtotime('1 Jan 2009');
fiscalYear = getFiscalYear($ date);
将返回2008
I am building a report for a client using 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
这篇关于Php财年报告,麻烦选择适当的年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!