2013-12-16 57 views
0

我想比較start dateend date,但它在同一個月工作。但我輸入兩個不同的月份是不行的。比較兩個日期不能正常工作

對於恩:我進入start date= 23/12/2013End date=06/12/2013它會工作,但我進入Start date = 23/12/2013End date=01/01/2014它會給同樣的錯誤。

型號:

public function _date(){ 
     $startDate = strtotime($this->input->post('start_date')); 
     $endDate = strtotime($this->input->post('end_date')); 

     if ($endDate >= $startDate) 
      return True; 
     else { 
      return False; 
      $this->form_validation->set_message('compareDate', '%s should be greater than Contract Start Date.'); 
     } 
     return False; 
} 

控制器:

function date_validation(){ 
    $isDate = $this->mdl_education->_date(); 
    $data['emp_id'] = $this->input->post('emp_id',TRUE); 
    if($isDate) 
    { 
     $this->session->set_flashdata('msg', 'Please Enter valid Date '); 
     redirect("education/form/".$data['emp_id']); 
    } 
} 



$this->form_validation->set_rules('end_date', 'End Date', 'trim|required|xss_clean|callback_date_validation'); 

什麼解決辦法嗎?

回答

0

試試這個

$startDate = strtotime(str_replace('/', '-', $this->input->post('start_date'))); 
$endDate = strtotime(str_replace('/', '-', $this->input->post('end_date'))); 

documentation for strtotime

更新:從上述文獻中第m

日期重要提示/ d/y或DMY格式通過查看在各個組件之間的隔板消除歧義:如果分離器是斜線(/) ,那麼假設美國的m/d/y;而如果分隔符是破折號( - )或點(。),則假定歐洲的d-m-y格式。

0

另一種選擇是DateTime

您必須提供您已設置適當的格式,否則將返回false例子是m/d/Y

$startDate = DateTime::createFromFormat('m/d/Y',$this->input->post('start_date'))->format('U'); 

$endDate = DateTime::createFromFormat('m/d/Y',$this->input->post('end_date'))->format('U'); 

你可以比較之前首先進行格式化爲timestamp

時間戳是自Unix紀元(1970年1月1日00:00:00 GMT)秒

if ($endDate >= $startDate) 
{ 
    return True; 
}else 
{ 
    return False; 
    $this->form_validation->set_message('compareDate', '%s should be greater than Contract Start Date.'); 
}