2014-09-24 69 views
0

輸入日期時,我沒有得到格式化日期。我需要它在年,月,日格式,但它是從月,日,年的形式輸入的。 我在搜索日期時遇到錯誤,因爲我無法按正確的順序進行搜索。我相信有一個簡單的答案,但我無法找到它。它在我手動設置日期時起作用。無法按照搜索的正確順序獲取日期

date is set like this by default. 
array(
    'month' => '12', 
    'day' => '01', 
    'year' => '2015' 
) 
    if ($this->request->is('post')) { 

     // debug($this->request->data); 
      $sdate=$this->request->data['Lesson']['lesson_date']; 
      $edate=$this->request->data['Lesson']['lesson_date2']; 
     // $sdate='2012-04-04'; //correct format 
     // $edate='2015-04-04'; 


       debug($sdate); 
      debug($edate); 

       $options['conditions'] = array('Tutor.id' => 2, 
       'and' => array(
         array('lesson_date >= ' => $sdate, 
           'lesson_date <= ' => $edate))); 


       $tutor=$this->set('tutor',$this->Lesson->find('all', $options)); 


     } 

//view 
    echo $this->Form->create(); 



echo $this->Form->input(
    'lesson_date', 
    array(
     'type' => 'date', 
     'selected' => array(
      'year'=>date('Y') 
     ), 
     'dateFormat' => 'YMD', 
     'minYear' => date('Y') , 
     'maxYear' => date('Y') +1 
    ) 
);   


echo $this->Form->input(
    'lesson_date2', 
    array(
     'type' => 'date', 
     'selected' => array(
      'year'=>date('Y') 
     ), 
     'dateFormat' => 'YMD', 
     'minYear' => date('Y') , 
     'maxYear' => date('Y') +1 
    ) 
); 

        echo $this->Form->end('Custom Search'); 

回答

1

加入我們的日期選項數組:dateFormat如果你想YMD格式

'dateFormat' => 'YMD' 

您可以設置dateformat到以下值: 'DMY', 'MDY', 'YMD' 或 '無' 。

More Detials

+0

我嘗試這樣做,它沒有work.Error:SQLSTATE [21000]:基數違規:1241操作數應包含1列(多個)。我在上面的問題中添加了一行。這個錯誤在於發現它不喜歡這個輸入,因此也是一個問題。我不應該得到-1,因爲它不是明顯的 – ajt 2014-09-24 10:25:02

+0

您還需要添加此行,否則它不會在查找中工作。您需要手動將日期解析爲字符串$ startd = $ sdate ['year']。 ' - '。$ sdate ['month']。 ' - '。$ sdate ['day']; – ajt 2014-09-24 11:54:32

相關問題