2012-05-27 63 views
1

我想使用php從MySQL表中獲取數據來計算財政年度。PHP計算財政年度

要求是計算每個財政年度(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

的 「test_date」;它的數據類型是什麼? – 2012-05-27 08:25:06

+0

2-sep-2011這種格式(varchar)(維護舊數據庫) – Harinder

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

我不知道還有什麼。

+0

是否會計算每年3月31日至4月1日之間的日期?是的第二個例子將工作2011年3月31日至2012年1月1日,但我必須改變每年的日期是否有任何東西會採取這些日期呢? – Harinder

+0

這裏的問題是它不是1月1日至31日.... – Harinder

+0

檢查編輯後 – 2012-05-27 08:58:07

4

當對日期進行計算時,擴展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
/** 
* 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;