我需要在 cakephp 中以一年的间隔获取记录。我尝试使用以下按time
条件查询不起作用。
$accept = $this->AssignListing->find('all',array('conditions' => array('AssignListing.writer_id' => $writer,'AssignListing.status' => 1, 'AssignListing.date_added > NOW( ) - INTERVAL 1 YEAR')));
在这种情况下不是更容易:
$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 中。
我认为这会解决问题,
$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'))))));
这对你有用。
$accept = $this->AssignListing->find('all',array('conditions' => array('AssignListing.writer_id' => $writer,'AssignListing.status' => 1, 'AssignListing.date_added >=' => 'DATE_ADD(NOW( ), INTERVAL -1 YEAR')));