Php code for validating date

In this example, the date fields will only accept input that matches the pattern 'dd/mm/yyyy' (this could just as easily be changed to 'yyyy-mm-dd' or 'mm/dd/yyyy').The time field will allow input starting with 'hh:mm' following by an optional 'am' or 'pm'. The code behind the form is as follows: For each field in the form (first the dates, then the time field), a check is made as to whether the input is blank.The below suggested date validation (from another note) does NOT work for ALL formats and date inputs.function validate Date($date, $format = 'Y-m-d H:i:s')A couple of examples should suffice as a warning to test carefully and particularly not to use code that allows passing in of just any date format string:var_dump(validate Date('7/01/16', 'n/j/y')); # false but should be truevar_dump(validate Date('7/1/16', 'm/d/y')); # false but should be true Sorry, I don't have a suggested correction.Input validation is performed to ensure only properly formed data is entering the workflow in an information system, preventing malformed data from persisting in the database and triggering malfunction of various downstream components.When capturing information for insertion into a database, or use in other processing, it's important to control what the user can enter.

You don't need to do that when using HTML 5 type="date" attribute (the format then will be forced to be YYYY-MM-DD).

/^(19[0-9]|2[0-9])\-(0[1-9]|1[0-2])\-(0[1-9]|1[0-9]|2[0-9]|3[0-1])((T|\s)(0[0-9]|1[0-9]|2[0-3])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])((\ |\.)[\d ])?

@whitej71 In the example you gave, it is correctly returning false.

It's so easily bypassed it should not be considered reliable.

It should only be considered an aid to the user, in that it helps them make fewer invalid form submissions. If you want in regex, try this, this will validate for, 01/01/1900 through 12/31/2099 Matches invalid dates such as February 31st Accepts dashes, spaces, forward slashes and dots as date separators $dt = '6/26/1970' ; // or // '6.26.1970' ; $dt = preg_replace("([.] )", "/", $dt); $test_arr = explode('/', $dt); if (checkdate($test_arr[0], $test_arr[1], $test_arr[2]) && preg_match("/[0-9]\/[0-9]\/[0-9]/", $dt)) else I tested it on Google Chrome and IE, everything works correctly.