Luodeni Posted March 5, 2009 Share Posted March 5, 2009 I created afunction which works with 3 dropdown menu's to get the date but no matter whether a date is a leapyear or not it always says it's an invalid date here's the code <?php function validDateCheck( $dobMonth, $dobDay, $dobYear ) { $message = ""; if ( $dobMonth == 2 || $dobMonth == 4 || $dobMonth == 6 || $dobMonth == 9 || $dobMonth == 11) { if ( $dobDay > 30) { $message .= $dobMonth . "-" . $dobDay . "-" . $dobYear . " is not a valid date. Please check and correct it! <br />"; } elseif ( $dobMonth == 2 && $dobDay > 28 ) { if ( ($dobMonth == 2) && ($dobDay == 29) && ($dobYear % 4 != 0) ) { $message .= $dobMonth . "-" . $dobDay . "-" . $dobYear . " is not a valid date. Please check and correct it! <br />"; } elseif ( ($dobMonth == 2) && ($dobDay >= 29) ) { $message .= $dobMonth . "-" . $dobDay . "-" . $dobYear . " is not a valid date. Please check and correct it! <br />"; } } } return $message; } ?> Link to comment https://forums.phpfreaks.com/topic/148046-solved-leap-year/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 5, 2009 Share Posted March 5, 2009 http://us.php.net/manual/en/function.checkdate.php Link to comment https://forums.phpfreaks.com/topic/148046-solved-leap-year/#findComment-777075 Share on other sites More sharing options...
Psycho Posted March 5, 2009 Share Posted March 5, 2009 Now, this will most likely not affect your particular application as you are using it for data of birth, but just for educational purposes: That leap year logic is not correct. Leap years do not occur every 4 years - exactly. There are exceptions to that. A year is a leap year if it is divisiable by 4 AND if is NOT divisiable by 100, UNLESS it is also divisable by 400. So every '00 year is not a leap year unless it is also divisable by 400. The year 2000 was a leap year, but hte year 2100 will not be. But, since you are using this for DOBs, the logic of just using divisable by four would cover everything from 1901 to 2099. Link to comment https://forums.phpfreaks.com/topic/148046-solved-leap-year/#findComment-777078 Share on other sites More sharing options...
Luodeni Posted March 5, 2009 Author Share Posted March 5, 2009 Now, this will most likely not affect your particular application as you are using it for data of birth, but just for educational purposes: That leap year logic is not correct. Leap years do not occur every 4 years - exactly. There are exceptions to that. A year is a leap year if it is divisiable by 4 AND if is NOT divisiable by 100, UNLESS it is also divisable by 400. So every '00 year is not a leap year unless it is also divisable by 400. The year 2000 was a leap year, but hte year 2100 will not be. But, since you are using this for DOBs, the logic of just using divisable by four would cover everything from 1901 to 2099. yes I was actually aware of that but as you already said it's only for birthdays and I don't know anyone who's 109 years old and still working hahah thanks to PFMaBiSmAd for the PHP checkdate() function. I ammended the codeto this now. which is much neater <?php function validDateCheck( $dobMonth, $dobDay, $dobYear ) { if (checkdate( $dobMonth, $dobDay, $dobYear) == true ) { $message .= ""; } else { $message .= $dobMonth . "-" . $dobDay . "-" . $dobYear . " Is not a valid date, please correct it!"; } return $message; } ?> Link to comment https://forums.phpfreaks.com/topic/148046-solved-leap-year/#findComment-777121 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.