PHP计算财务年度

问题描述 投票:1回答:5

我想用php来计算财务年度来从mysql表中获取数据。

要求是计算每个财政年度(3月31日至4月1日)的学生成绩。是否有可能每年自行计算这些日期的任何功能?

我的学生考试成绩表是存储测试日期(2-sep-2012),今年(2-sep-2011)的同一学生也有记录。我只想要当年的看跌期权。到现在为止我无法得到这个,这是我的代码: -

$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)'];

它给我的所有结果不是一个财政年度。

php function financial
5个回答
4
投票

在日期上执行计算时,扩展DateTime类是个好主意。这样可以将所有日期计算封装在一个地方。随着时间的推移,你将建立一个非常有用的库。

要计算会计年度,您可以延长DateTime: -

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;
    }
}

这将给出一个结果,您可以轻松地将其包含在您的查询中(如果可以,您应该真正更改为mysqli或pdo)。

您可以像这样使用新功能: -

$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']));

如果您希望可以修改方法以将开始日期和结束日期作为DateTime对象返回: -

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

然后,您可以直接格式化以包含在您的查询中: -

$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');

请参阅date formats的手册


0
投票

另一个:

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"); ?>')

我不知道还有什么。


0
投票
/**
* 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');

0
投票

这个帖子很老了,但我想在上面的vascowhites回答中添加我的改进。

以下课程将处理不同的会计年度,而不仅仅是北美默认。

输出也更灵活一些

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');

0
投票

这很容易

$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;
© www.soinside.com 2019 - 2024. All rights reserved.