Skip to content

Instantly share code, notes, and snippets.

@hallindavid
Created November 20, 2019 18:39
Show Gist options
  • Save hallindavid/1fbea98ae14be70607940889bdce870a to your computer and use it in GitHub Desktop.
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
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