2013-05-14 110 views
1

我试图在每月的日期范围内为每位客户查找所有服务。 在此之后回答问题CakePHP: Date Range我在总结控制器创建此代码:在日期范围内查找

 public function view($id=null) { 
      //other code 
      $this->loadModel('Event'); 
      $summary = $this->Summary->find('first', array('conditions' => array('Summary.id' => $id))); 
      $first = date('m-01-Y', strtotime($summary['Summary']['date'])); 

      //added this for debugging and the value is the date i want 
      $this->set('first', $first); 

      $last = date('m-t-Y', strtotime($summary['Summary']['date'])); 

      //debugging, this works too 
      $this->set('last', $last); 

      $conditions = array("'Event.start' >=" => $first, "'Event.start' <=" => $last, 
           'Event.customer_id' => 'Summary.customer_id' 
           ); 
      $this->set('events', $this->Event->find('all', array('conditions' => $conditions))); 

但在我看来,$事件变量是空的。我也试图消除customer_id条件来测试日期范围,问题似乎是'Event.start'无法正确比较。和date()函数不能用在引号内。任何提示?

回答

2

要添加更多的单引号您的查询将行不通

请如下更改为您$条件

$conditions = array("Event.start >=" => "'".$first."'", "Event.start <=" => "'".$last."'", 
           'Event.customer_id' => 'Summary.customer_id' 
           ); 

请让我知道如果我能帮助你更

$summary['Summary']['date'] = '4-5-2013'; // where format is 'd-m-Y'; 

$first = date('m-01-Y H:i:s', strtotime($summary['Summary']['date'])); 

echo "first".$first; 
echo "<br>date is -> ". date('m-t-Y'); 

输出像

first05-01-2013 00:00:00 
date is -> 05-31-2013 

所以你可以对比日期

+0

nope ..仍然是空的。顺便说一句,感谢您的快速回复! – 2013-05-14 09:45:39

+0

将请在这里定义你的日期格式 – liyakat 2013-05-14 10:05:07

+0

哦..也许你明白了。 event.start的格式是datetime,而summary.date只是一个日期。我会尝试将其更改为datetime并修改代码,然后我会通知您。 – 2013-05-14 10:11:31