我想从数据库中选择给定的日期间隔

时间:2017-05-23 08:16:41

标签: php codeigniter

我想从数据库中选择给定的日期间隔

  

$ last = date(" Y-m-t",strtotime(" 0 month"));
  $ start = date(" Y-m-1",   strtotime(" 0个月"));

我想选择 $ start $ last 之间的值,这是我的控制器

  

$收入= $这 - > select_model-> get_income(阵列(' i.date_of_income' = GT; $启动,    ' i.date_of_income' = GT; $最后));

这是我的模特: -

  

function get_income($ where = FALSE){
  $这 - > DB->选择(' *&#39);
  $ this-> db-> from(' unit u');
  $ this-> db->加入('收入i' u .registration_number = i.registration_number',' left');
  如果($哪里){
  $这 - > DB-化合物其中($其中);
  }
  $ query = $ this-> db-> get();
   return $ query-> result_array();
  }

3 个答案:

答案 0 :(得分:1)

对于间隔时间,您可以使用BETWEEN mysql关键字。

$incomes=$this->select_model->get_income("i.date_of_income BETWEEN '" . $start . "' AND '" . $last . "'");

答案 1 :(得分:1)

你可以使用它。 控制器查询

$incomes=$this->select_model->get_income($start,$last);

模型查询

function get_income($start=null,$last = null){
$this->db->select('*');
$this->db->from('unit u');
$this->db->join('income i','u.registration_number=i.registration_number','left');
if(!empey($start) && !empey($last){
$this->db->where('i.date_of_income >=', $first_date);
$this->db->where('i.date_of_income <=', $second_date);
}

$query = $this->db->get();
return $query->result_array();
}

OR

您只能更改控制器

$incomes = $this->select_model->get_income("i.date_of_income BETWEEN '{$start}' AND '{$last}'");

答案 2 :(得分:0)

像这样调用函数

$ incomes = $ this-&gt; select_model-&gt; get_income('i.date_of_income BETWEEN $ start AND $ last');