1) How to confirm the date using CI callbacks :
βThe validation system supports callbacks to your own validation functions. This allows you to extend the validation class to meet your needs.
In your method
$this->form_validation->set_rules('text_date', 'Date', 'trim|exact_length[10]|callback_validate_date');
Your own callback function:
public function validate_date($incoming_date) { //If in dd/mm/yyyy format if (preg_match("^\d{2}/\d{2}/\d{4}^", $incoming_date)) { //Extract date into array $date_array = explode('/', $incoming_date); //If it is not a date if(! checkdate($date_array[1], $date_array[0], $date_array[2])) { $this->form_validation->set_message('validate_date', 'Invalid date'); return false; } } //If not in dd/mm/yyyy format else { $this->form_validation->set_message('validate_date', 'Invalid date'); return false; } return true; }
2) How to compare two dates:
$date_one = (int) strtotime(str_replace('/', '-', $this->input->post('date_one', true))); $date_two = (int) strtotime(str_replace('/', '-', $this->input->post('date_two', true))); if($date_one < $date_two) { echo 'Message'; } else { echo 'Message'; }
source share