terungwa Posted July 26, 2014 Share Posted July 26, 2014 I have set up a function to validate user input date in yyyy-mm-dd format taking into account the following possibilities: the length of the month February 29th when the year is not a leap year When I run the code, I do not get any feedback whatsoever, when it should. This is the code below and I would appreciate your thoughts: if(!function_exists('checkdate')) { function checkdate($date, $checkyear, $currentmonth) { if($checkyear == 0)//if it is not a leap year { // if current month is february if($currentmonth == 2) { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|1[0-9]|2[0-8])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } // if current months are april, june, september or november elseif($currentmonth == 4 || $currentmonth == 6 || $currentmonth == 9 || $currentmonth ==11) { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|1[0-9]|2[0-8]|3[0])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } else { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-8]|3[01])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } } elseif($checkyear == 1)//if it is a leap year { // if current month is february if($currentmonth == 2) { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|1[0-9]|2[0-9])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } // if current months are april, june, september or november elseif($currentmonth == 4 || $currentmonth == 6 || $currentmonth == 9 || $currentmonth ==11) { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|1[0-9]|2[0-9]|3[0])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } else { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } } } $todate = getdate(); $currentmonth = $todate['mon']; $checkyear = date('L'); $date = 2014-02-31; echo checkdate($date, $checkyear, $currentmonth); Link to comment https://forums.phpfreaks.com/topic/290123-regex-function-to-match-a-date-in-yyyy-mm-dd-format-accounting-for-length-of-month/ Share on other sites More sharing options...
mac_gyver Posted July 26, 2014 Share Posted July 26, 2014 php has a function called checkdate(), which means your function definition isn't being made and your code is likely throwing php errors when you call php's built-in checkdate() function with those parameters. you need to ALWAYS have php's error_reporting set to E_ALL and have display_errors set to ON, when developing code, to get php to help you. Link to comment https://forums.phpfreaks.com/topic/290123-regex-function-to-match-a-date-in-yyyy-mm-dd-format-accounting-for-length-of-month/#findComment-1486189 Share on other sites More sharing options...
Jacques1 Posted July 27, 2014 Share Posted July 27, 2014 Besides that, the implementation is horrible. Sorry. Regexes are for patterns. They're not suitable for range checks. That's what we have the mathematical operators “<”, “<=”, “>” and “>=” for. It's also insane to do date calculations by hand. Even if you actually manage to find a PHP version which doesn't have checkdate(), the basic date functions are always available. I mean, handling dates is one of the core features of every language. And, yes, PHP can do it as well. Link to comment https://forums.phpfreaks.com/topic/290123-regex-function-to-match-a-date-in-yyyy-mm-dd-format-accounting-for-length-of-month/#findComment-1486222 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.