match_pattern[^([123]0|[012][1-9]|31)(/)(0[1-9]|1[012])(/)(19[0-9]{2}|2[0-9]{3})$]
// PHP function that tries to see if an inputed string is a valid date, // and if it is, returns it in the format yyyy-mm-dd - ready for MySQL. public function validate_date($date=null) { //if date is already in format 'yyyy-mm-dd' //then leave it as-is: if (preg_match('#^(\d{2}|\d{4})-\d{1,2}-\d{1,2}$#', $date)){ return $date; } //if date is in format 'dd/mm/yyyy', //or "dd.mm.yyyy" or "dd-mm-yyyy" //then convert it to 'yyyy-mm-dd', and return it: elseif (preg_match('#^(\d{1,2})[./-](\d{1,2})[./-](\d{2}|\d{4})$#', $date, $matches)) { return $matches[3].'-'.$matches[2].'-'.$matches[1]; } //if date is in any other format, fail validation: else { return false; } }
It looks like you're new here. If you want to get involved, click one of these buttons!