webdeveloper123 Posted July 24, 2022 Share Posted July 24, 2022 Hi Guys, My question is if I am doing something like this: $customers['first_name'] = $_POST['fname']; $customers['last_name'] = $_POST['lname']; $errors['first_name'] = is_text($customers['first_name'], 2, 20) ? '' : 'Must be 2-20 characters'; $errors['last_name'] = is_text($customers['last_name'], 2, 20) ? '' : 'Must be 2-20 characters'; How would I validate a date? There seems to be a function called checkdate and validatedate is mentioned (although no longer on php.net) so It must have been taken out. Btw, the date is not known before hand, as it's coming from a form which asks for users birthdate. Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/315084-how-to-validate-a-date-based-on-user-input-from-a-form/ Share on other sites More sharing options...
kicken Posted July 24, 2022 Share Posted July 24, 2022 You could use something like DateTime::createFromFormat() to try and parse the date. If it fails to parse, invalid format. If a valid date is created, you'll need to check it matches the input by comparing the input to the formatted date. This is because the function will "fix" invalid dates, for example 07/35/2022 would parse as 08/04/2022. <?php $input = '07/31/2022'; $date = DateTime::createFromFormat('m/d/Y', $input); if (!$date || $date->format('m/d/Y') !== $input){ echo 'Invalid date'; } else { echo 'Valid date'; } Any validate beyond that depends on what you need from the data. For a birthday for example, you might validate it's in the past and not in the future. Quote Link to comment https://forums.phpfreaks.com/topic/315084-how-to-validate-a-date-based-on-user-input-from-a-form/#findComment-1598573 Share on other sites More sharing options...
webdeveloper123 Posted July 24, 2022 Author Share Posted July 24, 2022 can I not do something like this, if all I want to do is make sure it is actually a valid date, nothing more $errors['birthdate'] = checkdate($customers['birthdate']) ? '' : 'Not a valid date'; Quote Link to comment https://forums.phpfreaks.com/topic/315084-how-to-validate-a-date-based-on-user-input-from-a-form/#findComment-1598586 Share on other sites More sharing options...
webdeveloper123 Posted July 24, 2022 Author Share Posted July 24, 2022 actually checkdate needs 3 parameters so that won't work Quote Link to comment https://forums.phpfreaks.com/topic/315084-how-to-validate-a-date-based-on-user-input-from-a-form/#findComment-1598587 Share on other sites More sharing options...
webdeveloper123 Posted July 24, 2022 Author Share Posted July 24, 2022 is there something like checkdate which will take 1 argument as a variable or as $customers['birthdate'] and see that the date is valid? Im looking around and can't fine one Quote Link to comment https://forums.phpfreaks.com/topic/315084-how-to-validate-a-date-based-on-user-input-from-a-form/#findComment-1598588 Share on other sites More sharing options...
Barand Posted July 24, 2022 Share Posted July 24, 2022 10 minutes ago, webdeveloper123 said: is there something like checkdate which will take 1 argument as a variable or as $customers['birthdate'] and see that the date is valid? Adapt @kicken's code function check_date($input, $format='m/d/Y') { $date = DateTime::createFromFormat($format, $input); return ($date && $date->format($format) === $input); } if (!check_date($input) ) { $error['date'] = 'Invalid date'; } Quote Link to comment https://forums.phpfreaks.com/topic/315084-how-to-validate-a-date-based-on-user-input-from-a-form/#findComment-1598589 Share on other sites More sharing options...
webdeveloper123 Posted July 25, 2022 Author Share Posted July 25, 2022 (edited) 17 hours ago, Barand said: $format='m/d/Y In my database I'm saving as 'Y-m-d' so would I update that line to reflect this? Edited July 25, 2022 by webdeveloper123 Quote Link to comment https://forums.phpfreaks.com/topic/315084-how-to-validate-a-date-based-on-user-input-from-a-form/#findComment-1598604 Share on other sites More sharing options...
Barand Posted July 25, 2022 Share Posted July 25, 2022 No. It is the expected input format that is being defined here. Consider changing the function thus function check_date($input, $format='m/d/Y') { $date = DateTime::createFromFormat($format, $input); return ($date && $date->format($format) === $input) ? $date->format('Y-m-d') : false; } It then returns "false" if the date is invalid but returns the date in Y-m-d format if valid. Quote Link to comment https://forums.phpfreaks.com/topic/315084-how-to-validate-a-date-based-on-user-input-from-a-form/#findComment-1598607 Share on other sites More sharing options...
webdeveloper123 Posted July 25, 2022 Author Share Posted July 25, 2022 thanks barand Quote Link to comment https://forums.phpfreaks.com/topic/315084-how-to-validate-a-date-based-on-user-input-from-a-form/#findComment-1598608 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.