Created
November 20, 2019 18:39
-
-
Save hallindavid/1fbea98ae14be70607940889bdce870a to your computer and use it in GitHub Desktop.
Codeigniter->form_validation->custom callback for check date which supports min date, max date and ensure formatted as YYYY-MM-DD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function check_date($str) | |
{ | |
$dts = date_create_from_format("Y-m-d", $str); | |
if ($dts == FALSE) | |
{ | |
$this->form_validation->set_message('check_date', 'The {field} field is in an unreadable format'); | |
return false; | |
} | |
$maxDate = date_create_from_format('Y-m-d', date('Y-m-d', strtotime('+1 day'))); | |
if ($dts > $maxDate) | |
{ | |
$this->form_validation->set_message('check_date', 'The {field} field cannot be in the future'); | |
return false; | |
} | |
$minDate = date_create_from_format('Y-m-d', date('Y-m-d', strtotime("-1 years"))); | |
if ($dts < $minDate) | |
{ | |
$this->form_validation->set_message('check_date', 'The {field} field cannot be more than 1 year ago'); | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment