Php 获取mysql数据的可重用函数

Php 获取mysql数据的可重用函数,php,zend-framework,Php,Zend Framework,我想写一个函数,它能够返回MySQL数据库中total\u exc列的总和。我已经写了一个函数,但它不可重用 该功能适用于今年第一季度 public static function getFirstQuarterTotalExcl(){ $first_day_of_1_quarter = date('Y-01-01'); $last_day_of_1_quarter = date('Y-03-31'); $query = "SELECT SUM(total_excl) F

我想写一个函数,它能够返回MySQL数据库中
total\u exc
列的总和。我已经写了一个函数,但它不可重用

该功能适用于今年第一季度

public static function getFirstQuarterTotalExcl(){
    $first_day_of_1_quarter = date('Y-01-01');
    $last_day_of_1_quarter = date('Y-03-31');
    $query = "SELECT SUM(total_excl) FROM ".Zim_Properties::getTableName('Invoice')." WHERE invoice_date BETWEEN '$first_day_of_1_quarter' AND '$last_day_of_1_quarter'";
    $total = Zend_Registry::get('db')->fetchAll($query);
    return round($total[0]["SUM(total_excl)"]);
}
本年度第二季度的第二项职能:

public static function getSecondQuarterTotalExcl(){
    $first_day_of_2_quarter = date('Y-04-01');
    $last_day_of_2_quarter = date('Y-06-30');
    $query = "SELECT SUM(total_excl) FROM ".Zim_Properties::getTableName('Invoice')." WHERE invoice_date BETWEEN '$first_day_of_2_quarter' AND '$last_day_of_2_quarter'";
    $total = Zend_Registry::get('db')->fetchAll($query);
    return round($total[0]["SUM(total_excl)"]);
}
如你所见。这段代码是不可重用的,这意味着每年每个季度我都必须编写一个新函数。我想每季度做一个函数。我应该怎么做才能使它可重复使用

我已经写了一个函数,但它不可重用

不清楚您在这里的意思,但如何将日期设置为函数接口的一部分,如下所示:

public static function getQuarterTotalExcl($first_day_of_quarter,$last_day_of_quarter){
    // $first_day_of_2_quarter = date('Y-04-01');
    // $last_day_of_2_quarter = date('Y-06-30');
    $query = "SELECT SUM(total_excl) FROM ".Zim_Properties::getTableName('Invoice')." WHERE invoice_date BETWEEN '$first_day_of_quarter' AND '$last_day_of_quarter'";
    $total = Zend_Registry::get('db')->fetchAll($query);
    return round($total[0]["SUM(total_excl)"]);
}
$first_quarter_results = getQuarterTotalExcl(date('Y-01-01'), date('Y-03-31'));
$second_quarter_results = getQuarterTotalExcl(date('Y-04-01'), date('Y-06-30'));
$third_quarter_results = getQuarterTotalExcl(date('Y-07-01'), date('Y-09-30'));
$fourth_quarter_results = getQuarterTotalExcl(date('Y-10-01'), date('Y-12-31'));
那么就这样称呼它:

public static function getQuarterTotalExcl($first_day_of_quarter,$last_day_of_quarter){
    // $first_day_of_2_quarter = date('Y-04-01');
    // $last_day_of_2_quarter = date('Y-06-30');
    $query = "SELECT SUM(total_excl) FROM ".Zim_Properties::getTableName('Invoice')." WHERE invoice_date BETWEEN '$first_day_of_quarter' AND '$last_day_of_quarter'";
    $total = Zend_Registry::get('db')->fetchAll($query);
    return round($total[0]["SUM(total_excl)"]);
}
$first_quarter_results = getQuarterTotalExcl(date('Y-01-01'), date('Y-03-31'));
$second_quarter_results = getQuarterTotalExcl(date('Y-04-01'), date('Y-06-30'));
$third_quarter_results = getQuarterTotalExcl(date('Y-07-01'), date('Y-09-30'));
$fourth_quarter_results = getQuarterTotalExcl(date('Y-10-01'), date('Y-12-31'));

将这两个日期作为参数传递给常规函数。
比如:

function getQuarterTotalExcl($day1, $day2)
{
    $query = "SELECT SUM(total_excl) FROM ".Zim_Properties::getTableName('Invoice')." WHERE invoice_date BETWEEN '$day' AND '$day2'";
    $total = Zend_Registry::get('db')->fetchAll($query);
    return round($total[0]["SUM(total_excl)"]);
}

是否将日期/季度作为参数发送,并且仅使用一个函数?使用php的日期函数,只需使用季度号就可以很容易地确定开始和结束日期。