2013-12-16 55 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.'); 
}