johnsmith153 Posted June 11, 2009 Share Posted June 11, 2009 I need a script to check if a date entered is valid (before then adding to db): Ie if user enters 01/01/2009 then this is ok, but if they enter loads of rubbish then shouldn't add to db I need in format dd/mm/yyyy. Is there a ready-made php function? Quote Link to comment https://forums.phpfreaks.com/topic/161826-check-if-date-is-valid/ Share on other sites More sharing options...
mattal999 Posted June 11, 2009 Share Posted June 11, 2009 You could do an explode on the date, like so: <?php // UNTESTED CODE $date = $_POST['date']; $expdate = explode("/", $date); if(count($expdate) == 3) { //Valid } else { //Invalid } ?> Just a nice lil way of checking if the string has 3 parts (Day, Month and Year). Quote Link to comment https://forums.phpfreaks.com/topic/161826-check-if-date-is-valid/#findComment-853815 Share on other sites More sharing options...
Adam Posted June 11, 2009 Share Posted June 11, 2009 Why not instead have 3 inputs and then join them together after validating each part? Other than that best choice is regex, something like: if (preg_match('/[0-3][0-9]\/[0-1][1-9]\/[1-2][0-9][0-9][0-9]/', $date)) { // valid } else { // invalid } Of course with that you run into issues with the user having to enter a forward slash between each part, when they could enter dot, hyphen, etc. Does it necessarily need to be a forward slash? Quote Link to comment https://forums.phpfreaks.com/topic/161826-check-if-date-is-valid/#findComment-853818 Share on other sites More sharing options...
PFMaBiSmAd Posted June 11, 2009 Share Posted June 11, 2009 You should use three dropdown/select menus so that the user has no choice about the format. You can then use the checkdate function to determine if the choice was valid (correct number of days in any month for that year.) Quote Link to comment https://forums.phpfreaks.com/topic/161826-check-if-date-is-valid/#findComment-853819 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.