Codeigniter在模型中减去时间并从数据库中进行选择

时间:2016-10-06 08:04:27

标签: php codeigniter codeigniter-2 codeigniter-3

我正在构建一个图书馆项目,用户需要知道哪些书籍没有按时提交。所以我已经将book_returning_date存储在数据库中了。但是我的问题是我需要知道如何选择通过与今天的日期比较,模型中的日期已经越过那里返回日期。我的模型代码如下:

 public function timecalculations($table){
        $query= $this->db->get_where('issue_books',array('department_id'=>$table));
        return $query->result();
    }

我的控制器如下:

public function latebooks(){

        $this->load->model('Time');
        $id=$this->session->userdata('userid');
        $this->load->model('Department');
        $table=$this->Department->selecttable($id);
        foreach($table as $q){}
        $table = $q->department_name;
        $table = strtolower($table);
        $run=$this->Time->timecalculations($table);
        $this->load->view('Books/datetime',['query'=>$run]);
    }

我是codeigniter的新手,所以请原谅,因为我可能对逻辑有点问题。提前谢谢!

4 个答案:

答案 0 :(得分:0)

添加另一个where子句,

public function timecalculations($table){
    $this->db->where('book_returning_date < NOW()');
    $query = $this->db->get($table);
    return $query->result();
}

这将返回当前日期已经超过book_returning_date的行 我假设您正在以下列格式保存book_returning_date'yyyy-mm-dd H:i:s'

答案 1 :(得分:0)

可能是这样的,对不起,如果我错了 首先需要在config中设置库会话设置 对于模型:

    public function timecalculations($table){
        $condition = 'department_id = "'.$table.'"';
        $this->db->select('issue_books');
        $this->db->where($condition);
        $query= $this->db->get();
        return $query->result();
    }

对于控制器我不确定但是喜欢这样:

    public function __construct() {
        $this->load->model('Time', 'time');
        $this->load->model('Department', 'departement');
        $this->load->library('Session');
    }

    public function latebooks($id){
        $id=$this->session->userdata('userid');
        $table=$this->department->selecttable($id);
        foreach($table as $q){ (probably i'm wrong)
            $table = $q->department_name; (probably i'm wrong)
        }   (probably i'm wrong)
        $table = strtolower($table); (probably i'm wrong)
        $run=$this->time->timecalculations($table);
        $this->load->view('Books/datetime',['query'=>$run]);
    }

答案 2 :(得分:0)

试试这个

 public function timecalculations($table){
        $query= $this->db->where('book_returning_date < CURRENT_DATE()', NULL, FALSE)->get($table);
        return $query->result();
    }

答案 3 :(得分:0)

试试这个

在控制器中

public function latebooks()
{
    $this->load->model('Time');
    $this->load->model('Department');

    $id = $this->session->userdata('userid');    
    $result = $this->Department->selecttable($id);  # Changed 

    $table = $result[0]['department_name']; # Changed 
    $table = strtolower($table); # Changed 

    $run = $this->Time->timecalculations($table);
    $this->load->view('Books/datetime',['query'=>$run]);
}

在模型中

public function timecalculations($table)
{
    $query = $this->db->get_where('issue_books',array('department_id'=>$table));
    $result =  $query->result_array();
    return $result;
}