3

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

要求是计算每个财政年度(3 月 31 日至 4 月 1 日)的学生分数。是否可以制作任何每年自行计算这些日期的函数?

我的学生考试成绩表正在存储考试日期(2012 年 9 月 2 日),并且今年(2011 年 9 月 2 日)的同一学生也有旧记录。我只想输出当年的。直到现在我无法得到这个,这是我的代码:-

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

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

4

6 回答 6

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

有关日期格式,请参阅手册

于 2012-05-27T11:43:40.520 回答
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');
于 2014-02-10T10:43:07.703 回答
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"); ?>')

我不知道还有什么。

于 2012-05-27T08:44:27.773 回答
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');
于 2015-09-28T11:39:58.793 回答
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;
于 2018-03-06T09:49:49.490 回答
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'));
}

?>

于 2021-06-14T05:24:40.717 回答