codeigniter获取最后1分钟的数据库字段


codeigniter get database field of last 1 minute

我正在尝试获取最近1分钟的数据库结果。。数据库如下所示:

id| from | to  | username | status | dbtime
-------------------------------------------------------
1 | u_1  | v_2 | myname   | 0      | 2015-02-19 14:30:0
2 | u_1  | v_2 | myname1  | 1      | 2015-02-19 14:30:0
3 | u_3  | v_1 | myname2  | 0      | 2015-02-19 16:30:0
4 | u_1  | v_2 | myname3  | 1      | 2015-02-19 16:30:0
5 | u_1  | v_2 | myname4  | 0      | 2015-02-19 16:30:0

假设当前时间为2015-02-19 16:30:0。。我的目标是从数据库中获取id号4,因为它在当前时间和状态为1。。这是我的问题:

$this->db->select();
$this->db->order_by('dbtime', 'asc');
$result = $this->db->get_where('dbname', array('to' => $someid, 'status' => 1, 'dbtime >=' => time()- 90, ),1 );
if ($result->num_rows() > 0)
{
    return 1;
}
else {
  return 0;
}

但这个查询并没有给我预期的结果。它返回给我的是数据库列id 2,而不是id no 4。。。

代码不起作用的原因是您试图比较两种不同的日期格式。Mysql将需要看到与存储内容等效的数据,以便运行比较。这就是我要做的:

function getData($someid) {
    $curr = date('Y-m-d H:i:s');
    $last_min = date('Y-m-d H:i:s', strtotime('-1 minutes'));
    $this->db->select();
    $this->db->from('dbname');
    $this->db->where('to', $someid);
    $this->db->where('status', 1);
    $this->db->where('dbtime >=', $last_min);
    $this->db->where('dbtime <=', $curr);
    $this->db->order_by('dbtime', 'asc');
    $result = $this->db->get();
    if ($result->num_rows() > 0) {
        return 1;
    } else {
        return 0;
    }
}

试试这个

$this->db->select();
$this->db->order_by('dbtime', 'asc');
$result = $this->db->get_where('dbname', array('to' => $someid, 'status' => 1, 'dbtime' => date_sub(now(), interval 1 minute)),1 );
if ($result->num_rows() > 0)
{
    return 1;
}
else {
  return 0;
}