计算时间并将其与PHP中的mysql时间进行比较

时间:2012-07-29 07:06:36

标签: php mysql codeigniter datetime unix-timestamp

PHP:

$current = time();
$time_ago = $current - 45*60; // get the time 45 mts ago.

MySQL的:

我有一个表状态,其日期时间列'已修改';

我正在尝试查询表,以获得修改时间低于$time_ago;的行

我的PHP代码:

$this->db->select("(SELECT UNIX_TIMESTAMP(status.modified) FROM status WHERE status_id = 0) AS modified_timestamp" , FALSE);
        $this->db->from('status');
        $this->db->where('status_id', 0);
        $this->db->where('modified_timestamp <', $time_ago);
        $this->db->limit(25);
        $q = $this->db->get();

获取数据库错误:

  

'where子句'中的未知列'modified_timestamp'

SELECT 
    (SELECT UNIX_TIMESTAMP(status.modified) FROM status WHERE status_id = 0) AS modified_timestamp 
    FROM (status) WHERE `status_id` = 0 AND `modified_timestamp` < 1343542758 LIMIT 25

2 个答案:

答案 0 :(得分:0)

$this->db->from('status');
$this->db->where('status_id', 0);
$this->db->where('UNIX_TIMESTAMP(modified) <', $time_ago);
$this->db->limit(25);

答案 1 :(得分:0)

你意识到你可以在where子句中使用UNIX_TIMESTAMP(),对吗?

$this->db->select("UNIX_TIMESTAMP(modified)" , FALSE);
$this->db->from('status');
$this->db->where('status_id', 0);
$this->db->where('UNIX_TIMESTAMP(modified) <', $time_ago);
$this->db->limit(25);
$q = $this->db->get();

注意:可能无法正常工作,我没有使用过这个特定的数据库API,但是你明白了。