Reputation: 706
I need to fetch records within interval of an year in cakephp. I tried with the following query by time
condition is not working.
$accept = $this->AssignListing->find('all',array('conditions' => array('AssignListing.writer_id' => $writer,'AssignListing.status' => 1, 'AssignListing.date_added > NOW( ) - INTERVAL 1 YEAR')));
Upvotes: 1
Views: 861
Reputation: 5464
This will work for you.
$accept = $this->AssignListing->find('all',array('conditions' => array('AssignListing.writer_id' => $writer,'AssignListing.status' => 1, 'AssignListing.date_added >=' => 'DATE_ADD(NOW( ), INTERVAL -1 YEAR')));
Upvotes: 1
Reputation: 9616
I think this will do the trick,
$accept = $this->AssignListing->find('all',array('conditions' => array(
'AssignListing.writer_id' => $writer,
'AssignListing.status' => 1,
array('AssignListing.date_added BETWEEN ? AND ?' => array(date('Y-m-d', strtotime("-1 Year")), date('Y-m-d'))))));
Upvotes: 1
Reputation: 10996
Isn't it easier in this case to just:
$str_time = date('Y-m-d H:i:s', strtotime('-1 Year'));
$accept = $this->AssignListing->find('all',array('conditions' => array(
'AssignListing.writer_id' => $writer,
'AssignListing.status' => 1,
'AssignListing.date_added >' => $str_time)));
And compare that value? When possible, do the calcuation once in php and parse it instead of in MySQL.
Upvotes: 1