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

それは私に1会計年度ではなくすべての結果を与えてくれます。

4

6 に答える 6

4

日付の計算を実行する場合は、DateTimeクラスを拡張することをお勧めします。これにより、すべての日付計算がカプセル化され、1 か所にまとめられます。時間が経つにつれて、非常に便利なライブラリが構築されます。

会計年度を計算するには、次のように 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

簡単です

$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 に答える