doubledee Posted August 21, 2011 Share Posted August 21, 2011 Currently I have an Input field for a "Written On Date". What is the best way to make sure a valid date is entered? Should I use Regular Expressions? Some PHP Date-Checking Function? Or switch to Drop-Down Lists to better control the selectable values? (Which won't catch things like June 31?!) Debbie Link to comment https://forums.phpfreaks.com/topic/245331-best-way-to-check-a-date/ Share on other sites More sharing options...
joel24 Posted August 21, 2011 Share Posted August 21, 2011 use jquery UI's datepicker rather than letting users type a date... and then you know the format the date will be, if it's not, return an error Link to comment https://forums.phpfreaks.com/topic/245331-best-way-to-check-a-date/#findComment-1260046 Share on other sites More sharing options...
doubledee Posted August 21, 2011 Author Share Posted August 21, 2011 use jquery UI's datepicker rather than letting users type a date... and then you know the format the date will be, if it's not, return an error And when they have JavaScript disabled... Debbie Link to comment https://forums.phpfreaks.com/topic/245331-best-way-to-check-a-date/#findComment-1260047 Share on other sites More sharing options...
PFMaBiSmAd Posted August 21, 2011 Share Posted August 21, 2011 checkdate Link to comment https://forums.phpfreaks.com/topic/245331-best-way-to-check-a-date/#findComment-1260048 Share on other sites More sharing options...
doubledee Posted August 21, 2011 Author Share Posted August 21, 2011 checkdate Per that page... Anonymous 13-Jul-2009 07:06 Beware that checkdate can not be used as validation to check if a date is correct. Debbie Link to comment https://forums.phpfreaks.com/topic/245331-best-way-to-check-a-date/#findComment-1260049 Share on other sites More sharing options...
joel24 Posted August 21, 2011 Share Posted August 21, 2011 you could have three select elements with year, month, day and then check is_numeric() on each of the fields to ensure validity <select name='year'> <?php for ($i=1990;$i<2020;$i++) { echo "<option value='$i'>$i</option>"; } ?> </select> //do again for month, day... though month you would have the value as the numeric month and the displayed value as the month name Link to comment https://forums.phpfreaks.com/topic/245331-best-way-to-check-a-date/#findComment-1260065 Share on other sites More sharing options...
PFMaBiSmAd Posted August 21, 2011 Share Posted August 21, 2011 The Anonymous post on that page is correct. Checkdate, the way he tried to use it, should not be used as validation to check if a date is correct (the format or if it contains any XSS scripting.) That's not its' job. The point of checkdate is to validate if the month, day, and year numerical values it is passed is a valid date (i.e. One of the things you asked - What is the best way to make sure a valid date is entered?) The preg_split pattern that he used isn't specific to the expected data, so of course it is possible to bypass it. Regardless if using one field for the date (a datepicker) or three different fields (three dropdown select lists, one each for the month, day, and year), in real life, you would use a pattern that matched the format of the data you are expecting. Link to comment https://forums.phpfreaks.com/topic/245331-best-way-to-check-a-date/#findComment-1260070 Share on other sites More sharing options...
doubledee Posted August 21, 2011 Author Share Posted August 21, 2011 The Anonymous post on that page is correct. Checkdate, the way he tried to use it, should not be used as validation to check if a date is correct (the format or if it contains any XSS scripting.) That's not its' job. The point of checkdate is to validate if the month, day, and year numerical values it is passed is a valid date (i.e. One of the things you asked - What is the best way to make sure a valid date is entered?) The preg_split pattern that he used isn't specific to the expected data, so of course it is possible to bypass it. Regardless if using one field for the date (a datepicker) or three different fields (three dropdown select lists, one each for the month, day, and year), in real life, you would use a pattern that matched the format of the data you are expecting. So I take my Month, Day, and Year, run them through checkdate() and if I get a "True" then I'm good to go?! Debbie Link to comment https://forums.phpfreaks.com/topic/245331-best-way-to-check-a-date/#findComment-1260154 Share on other sites More sharing options...
darkfreaks Posted August 21, 2011 Share Posted August 21, 2011 try something like this function is_date( $str ) { $stamp = strtotime( $str ); if (!is_numeric($stamp)) { return FALSE; } $month = date( 'm', $stamp ); $day = date( 'd', $stamp ); $year = date( 'Y', $stamp ); if (checkdate($month, $day, $year)) { return TRUE; } return FALSE; } Link to comment https://forums.phpfreaks.com/topic/245331-best-way-to-check-a-date/#findComment-1260158 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.