cherylinn Posted June 22, 2012 Share Posted June 22, 2012 how to write the code for date validation where leap year etc is taken into account, there shall not be any non-numeric number in the year and year must not be <1900, and the month and day must not be empty . My code is $day=$_POST['day'] $month=$_POST['month'] $year=$_POST['year'] Quote Link to comment https://forums.phpfreaks.com/topic/264626-validating-date/ Share on other sites More sharing options...
Mahngiel Posted June 22, 2012 Share Posted June 22, 2012 Post code, not homework. And see checkdate() Quote Link to comment https://forums.phpfreaks.com/topic/264626-validating-date/#findComment-1356216 Share on other sites More sharing options...
cherylinn Posted June 22, 2012 Author Share Posted June 22, 2012 thanks Quote Link to comment https://forums.phpfreaks.com/topic/264626-validating-date/#findComment-1356219 Share on other sites More sharing options...
Barand Posted June 22, 2012 Share Posted June 22, 2012 Needs to be integer number for the month and not a string Quote Link to comment https://forums.phpfreaks.com/topic/264626-validating-date/#findComment-1356220 Share on other sites More sharing options...
Psycho Posted June 22, 2012 Share Posted June 22, 2012 $day = isset($_POST['day']) ? (int) $_POST['day'] : 0; $month = isset($_POST['month']) ? (int) $_POST['month'] : 0; $year = isset($_POST['year']) ? (int) $_POST['year'] : 0; if(checkdate($month, $day, $year)) { echo "Valid date"; } else { echo "Invalid date"; } Quote Link to comment https://forums.phpfreaks.com/topic/264626-validating-date/#findComment-1356223 Share on other sites More sharing options...
PFMaBiSmAd Posted June 22, 2012 Share Posted June 22, 2012 To test the year for the <1900 condition, you would write a simple if(){} conditional statement. Quote Link to comment https://forums.phpfreaks.com/topic/264626-validating-date/#findComment-1356225 Share on other sites More sharing options...
Psycho Posted June 22, 2012 Share Posted June 22, 2012 To test the year for the <1900 condition, you would write a simple if(){} conditional statement. Good catch PFMaBiSmAd, I forgot that. Although if really old dates are not allowed there might also be a requirement not to allow future dates - but that wasn't mentioned. Quote Link to comment https://forums.phpfreaks.com/topic/264626-validating-date/#findComment-1356262 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.