2014-09-24 77 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