Date validation usually means regular expressions, and regular expressions usually means headache. All in all, I do think that PHP’s date handling is pretty sweet, but native validation is still lacking. With PHP 5.3 though, we are one step closer thanks to a new function with the infinitely long name date_parse_from_format. I’m going to show you how it can be used to create a validation function for any PHP date format.
So, the date_parse_from_format takes a date format and a string and parses the date to an associative array. Along with this I used the native checkdate which takes the argument month, day and year and tells you whether or not it is a valid date.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
The valid_date function can be used by anyone sporting a PHP 5.3 install on their web host. Since I’m using CodeIgniter I simply put this function in MY_Form_Validation, which means I can easily use this function like any standard CI validation function, i.e.
1
|
|
This is a really handy trick for those otherwise (for me, at least) nasty date validations.