SchweppesAle Posted January 2, 2010 Share Posted January 2, 2010 hi, I'm trying to use the following function in order to validate input as it's entered into the database. All input should follow the date('Y-m-d') format. If the function returns false I just enter a default value. Seems there's something wrong with the following algorithm though. /*make sure date is valid and follows Y-m-d format*/ function dateValidation($date) { /*first array element must be between 1970 and 2038*/ /*second element must be between 01 and 12*/ /*third between 01 and 31*/ $validity = TRUE; $date = explode('-', $date); if($date[0] > 2038 || $date[0] < 1970) { $validity = FALSE; } if($date[1] > 12 || $date[1] < 01) { $validity = FALSE; } if($date[2] > 31 || $date[2] < 01) { $validity = FALSE; } /*must be equal to 3*/ $size = count($date); if($size != 3) { $validity = FALSE; } return $validity; } Link to comment https://forums.phpfreaks.com/topic/186903-timestamp-format-validation/ Share on other sites More sharing options...
SchweppesAle Posted January 2, 2010 Author Share Posted January 2, 2010 tried a new approach, still no luck though. /*make sure date is valid and follows Y-m-d format*/ function dateValidation($date) { $validity = TRUE; $date = explode('-', $date); /*must be equal to 3*/ $size = count($date); if($size != 3) { $validity = FALSE; } if(!checkdate($date[1], $date[2], $date[0])) { $validity = FALSE; } return $validity; } Link to comment https://forums.phpfreaks.com/topic/186903-timestamp-format-validation/#findComment-987018 Share on other sites More sharing options...
rajivgonsalves Posted January 2, 2010 Share Posted January 2, 2010 function dateValidation($date) { @list ($year, $month, $day) = explode('-', $date); return (strlen($year) === 4 && strlen($month) === 2 && strlen($day) === 2) ? checkdate($month,$year,$day) : false; } Link to comment https://forums.phpfreaks.com/topic/186903-timestamp-format-validation/#findComment-987024 Share on other sites More sharing options...
SchweppesAle Posted January 2, 2010 Author Share Posted January 2, 2010 function dateValidation($date) { @list ($year, $month, $day) = explode('-', $date); return (strlen($year) === 4 && strlen($month) === 2 && strlen($day) === 2) ? checkdate($month,$year,$day) : false; } still no good :/ It only seems to work when the field is left blank. Link to comment https://forums.phpfreaks.com/topic/186903-timestamp-format-validation/#findComment-987028 Share on other sites More sharing options...
rajivgonsalves Posted January 2, 2010 Share Posted January 2, 2010 how are you calling the function consider the following <?php function dateValidation($date) { @list ($year, $month, $day) = explode('-', $date); return (strlen($year) === 4 && strlen($month) === 2 && strlen($day) === 2) ? checkdate($month,$year,$day) : false; } var_dump(dateValidation("")); ?> Link to comment https://forums.phpfreaks.com/topic/186903-timestamp-format-validation/#findComment-987029 Share on other sites More sharing options...
SchweppesAle Posted January 2, 2010 Author Share Posted January 2, 2010 whoops. nevermind, seems both algorithms work. I goofed >< if($endDate) { $endDate = '1970-01-31'; } instead of if(!$this->dateValidation($endDate)) { $endDate = '1970-01-31'; } Link to comment https://forums.phpfreaks.com/topic/186903-timestamp-format-validation/#findComment-987030 Share on other sites More sharing options...
SchweppesAle Posted January 2, 2010 Author Share Posted January 2, 2010 thanks by the way Link to comment https://forums.phpfreaks.com/topic/186903-timestamp-format-validation/#findComment-987031 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.