2012-08-06 84 views
1

我需要在cakephp中的一年間隔內抓取記錄。我試着用time以下查詢條件不起作用。在cakephp中獲取當前年份的mysql記錄

$accept = $this->AssignListing->find('all',array('conditions' => array('AssignListing.writer_id' => $writer,'AssignListing.status' => 1, 'AssignListing.date_added > NOW() - INTERVAL 1 YEAR')));

回答

1

這會爲你工作。

$accept = $this->AssignListing->find('all',array('conditions' => array('AssignListing.writer_id' => $writer,'AssignListing.status' => 1, 'AssignListing.date_added >=' => 'DATE_ADD(NOW(), INTERVAL -1 YEAR'))); 
1

是不是在這種情況下只是更容易:

$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))); 

和比較價值?如果可能的話,在php中進行一次計算並解析它,而不是在MySQL中。

strtotime()
date()

1

我認爲這會做的伎倆,

$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')))))); 
相關問題