There are various ways of validating dates in PHP or MySql, sometimes however you may just need to check if a string is a valid MySQL format date, outside of a form validator. This simple utility function will accept a string returning true if it’s a valid MySQL date, using a regular expression.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * Check if a date is in mysql format. * * @param string $date - e.g. 2012-09-12 * @return bool */ function isDateMySqlFormat($date) { if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $date)) return true; else return false; } |