Search code examples
phpmysqlcodeigniterdatecodeigniter-2

Query for rows added within 15 minutes


I have a date/time saved in the entry_date column of my database - Example: 1436459520

I am trying to grab only the entries that where added in the last 15 minutes.

I thought the following would work, but nothing is returned.

$this->EE->db->select('entry_id, entry_date')
                     ->from('exp_channel_titles')
                     ->where('FROM_UNIXTIME("entry_date") >= DATE_SUB(CURDATE(), INTERVAL 15 MINUTE)', NULL, FALSE);

Firstly, I notice that my dates are being saved as GMT, how can I ensure the current date is GMT to?

Can anyone please tell me if there is anything particularly else wrong here?

Update

I have tried:

FROM_UNIXTIME("entry_date") >= (now() - interval 15 minute)

Which also doesn't work.


Solution

  • Try:

    ->where('`entry_date` >= UNIX_TIMESTAMP(NOW())')
    

    The unix timestamps are offset from '1970-01-01 00:00:00' UTC so the timezones will be not be an issue in that format.