我想使用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)'];
它给我的所有结果不是一个财政年度。
答案 0 :(得分: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
手册答案 1 :(得分: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"); ?>')
我不知道还有什么。
答案 2 :(得分: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');
答案 3 :(得分: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');
答案 4 :(得分: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;
答案 5 :(得分: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'));
}
?>