2015-02-09 64 views
0

我正在嘗試創建get語句函數,並且在定義日期範圍時遇到了一些問題。 ?在cakephp中檢索基於年份和月份的數據

的數據將通過以下列格式get方法傳遞:年= YYYY &一個月=毫米 表的相關列有日期時間型(2012-02-01)。

我檢查了TimeHelper類,更特別是TimeHelper::daysAsSql($begin, $end, $fieldName, $timezone = NULL),但我不確定它是否適用於這種情況。

我在想如果我可以使用年份和月份這兩個變量創建Y-m格式的日期,然後在檢索數據的條件下使用它,但我確信有更高效和正確的方法。誰能幫忙?

$user_id = $Client['Client']['id']; 
    $year = $this->request['url']['year']; 
    $month = $this->request['url']['month']; 
    //$date_created = date('Y-m'); 


    if (!empty($user_id) && (!empty($date_created))) { 


     $Reports = $this->Report->find('all', array(
     'fields' => array('amount','currency','date_created','invoice_no'), 
     'conditions' => array(
      'Report.client_id'=> $user_id, 
      'Report.date_created >=' => $date_created, 
      'Report.date_created <=' => $date_created 

      ), 
     ) 
    ); 
+0

根據您的樣品中的條件下,如果Report.date_created等於$ DATE_CREATED它只會工作。因爲它不能小於,並且同時變大。你的意思是有其他條件,如date_start和date_end在那裏? – 2015-02-10 01:16:37

回答

1

(dateColumn)返回表示指定日期的月份的整數。

年份(dateColumn)返回一個整數,表示指定日期的年份。

InCakephp用作

$user_id = $Client['Client']['id']; 
    $year = $this->request['url']['year']; 
    $month = $this->request['url']['month']; 
    //$date_created = date('Y-m'); 


    if (!empty($user_id) && (!empty($date_created))) { 


     $Reports = $this->Report->find('all', array(
       'fields' => array('amount','currency','date_created','invoice_no'), 
       'conditions' => array(
         'Report.client_id '=> $user_id, 
         'MONTH(date_created) >='=> $month, 
         'YEAR(date_created) <=' => $year 

       ), 
     ) 
     ); 
+0

感謝您的回答,非常有幫助。因此,從日期欄中使用MONTH和YEAR,我可以在cakephp中編寫一些內容嗎? SELECT * FROM'Report' WHERE strftime('%m','date_created')= $ month AND strftime('%y','date_created')= year – abcy122 2015-02-10 09:11:46

+0

SQLite中的STRFTIME()類似於DATE_FORMAT具有反向參數的MySQL。 由於%d和%m映射到兩者中的相同事物,因此您的表達式可以簡單地寫爲; WHERE DATE_FORMAT(date_created,'%m')= ....... – Supravat 2015-02-10 16:34:52