Hooman Ahmadi
Hooman Ahmadi

Reputation: 1397

comparing a DateTime in CakePHP

I have a query in CakePHPthat has a stored "datetime" field called DropIn.drop_in_time. I would like to only "find" entries where the DropIn.drop_in_time is > NOW() but am having trouble getting it to do that.

The condition DropIn.drop_in_time >' => 'NOW() didn't get the right results in the query below. Is there a better way to do it?

$requests = $this->DropIn->find('all', array(
            'conditions' => array('DropIn.drop_in_time >' => 'NOW()', 'or' => array(array('DropIn.user_id' => $this->Auth->user('id')), array('DropIn.id' => $drop_in_ids))),
            'order'=>array('DropIn.created'=>'DESC')));

Upvotes: 4

Views: 7203

Answers (3)

kekin
kekin

Reputation: 1

use DboSource::expression('NOW') instead of only NOW()

Upvotes: 0

Dunhamzzz
Dunhamzzz

Reputation: 14808

If you really want to put DB expressions into CakePHP finds, you can use the expression method:

'DropIn.drop_in_time.' => $db->expression('CURDATE()'); 

However this is kinda of losing the point of the database abstraction that a framework provides, so do as suggested by deceze and compare it with date('Y-m-d H:i:s')

Upvotes: 0

deceze
deceze

Reputation: 522626

If you separate the value as 'DropIn.drop_in_time' => 'NOW()', 'NOW()' is taken to mean the literal value string "NOW()". Just write it as one SQL fragment instead: 'DropIn.drop_in_time > NOW()'. Alternatively, use 'DropIn.drop_in_time >' => date('Y-m-d H:i:s').

Upvotes: 9

Related Questions