2013-05-14 114 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