Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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')));
like image 624
Hooman Ahmadi Avatar asked Jan 20 '23 08:01

Hooman Ahmadi


1 Answers

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').

like image 157
deceze Avatar answered Jan 30 '23 08:01

deceze