如何在codeigniter中获取每日,每周和每月报告

时间:2016-12-19 15:38:23

标签: php codeigniter

public function index(){
    $this->load->model(array('msellproduct','minvoice'));
    $invoice=$this->minvoice->get_many_by(array('vendor_id'=>$this->cuseridentity->get_data( 'id' ),'create_time'=>date("Y-m-d")));
    $week=$this->minvoice->get_many_by(array('vendor_id'=>$this->cuseridentity->get_data( 'id' ),'create_time' =>" DATE_SUB(".date('Y-m-d').",INTERVAL 1 WEEK)"));
    $month=$this->minvoice->get_many_by(array('vendor_id'=>$this->cuseridentity->get_data( 'id' ),'create_time'=>"DATE_SUB(".date("Y-m-d").",INTERVAL 1 Month"));
    $product=$this->msellproduct->get_many_by(array('vendor_id'=>$this->cuseridentity->get_data( 'id' )));
    var_dump($week);
    $this->_render( 'index', array(
        'model' => null,
        'product'=>$product,
        'invoice'=>$invoice,
        'week'=>$week,
        'month'=>$month,
    ) );
}

在上面的功能中,我收到了每日报告。这是正确的,但我没有收到每周和每月的报告。

1 个答案:

答案 0 :(得分:0)

使用PHP获取并转换日期并将(通过)日期而不是SQL日期函数。 例如:

public function index(){
    $this->load->model(array('msellproduct','minvoice'));
    $invoice=$this->minvoice->get_many_by(array('vendor_id'=>$this->cuseridentity->get_data( 'id' ),'create_time'=>date("Y-m-d")));
    //Calculate date here if your SQL is taking just date
    $week_date = strtotime(date("Y-m-d"))-(7*24*60*60);
    $month_date = strtotime(date("Y-m-d"))-(30*24*60*60); //you can get days in the current month too
    $week=$this->minvoice->get_many_by(array('vendor_id'=>$this->cuseridentity->get_data( 'id' ),'create_time' =>date("Y-m-d",$week_date)));
    $month=$this->minvoice->get_many_by(array('vendor_id'=>$this->cuseridentity->get_data( 'id' ),'create_time'=>date("Y-m-d",$month_date)));
    $product=$this->msellproduct->get_many_by(array('vendor_id'=>$this->cuseridentity->get_data( 'id' )));
    var_dump($week);
    $this->_render( 'index', array(
        'model' => null,
        'product'=>$product,
        'invoice'=>$invoice,
        'week'=>$week,
        'month'=>$month,
    ) );
}

最有可能你的查询需要日期,所以简单地使用PHP计算日期并将该日期传递给SQL,如果你的日期参数与日常参数相同。根据每日报告,我得到的是你的SQL只是一个日期。以你的方式也可以这样做,但是现在没有你的SQL代码就很容易做到这一点。