PRodgers4284 Posted February 21, 2008 Share Posted February 21, 2008 I have a form that check the format of a date but i need it to check whether the date is equal to the todays current date or after. I dont what the users to be able to enter a date that has already past, can anyone help? I have the following code: //Date check) if (empty($date)) { //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter a dob $date_message = '*Please enter a closing date*'; } //Check the format and explode into $parts elseif (!ereg("^([0-9]{2})/([0-9]{2})/([0-9]{4})$", $date, $parts)){ $error_stat = 1; //Set the message to tell the user the date is invalid $date_message = '*Invalid date, must be DD/MM/YYYY format*'; } elseif (!checkdate($parts[2],$parts[1],$parts[3])) { $error_stat = 1; //Set the message to tell the date is invalid for the month entered $date_message = '*Invalid date, month must be between 1-12*'; } elseif (intval($parts[3]) < 2008 || intval($parts[3]) > intval(date("Y"))) { $error_stat = 1; //Set the message to tell the user the date is invalid for the year entered $date_message = '*Invalid date*'; } Quote Link to comment Share on other sites More sharing options...
revraz Posted February 21, 2008 Share Posted February 21, 2008 Convert your MM/DD/YYYY into YYYY-MM-DD and you can do a compare to NOW using MySQL or convert to Unix time and you can compare to date() Quote Link to comment Share on other sites More sharing options...
PRodgers4284 Posted February 21, 2008 Author Share Posted February 21, 2008 have do i compare the dates, will it require alot of changes? Quote Link to comment Share on other sites More sharing options...
revraz Posted February 21, 2008 Share Posted February 21, 2008 You compare by simply doing if $date >= NOW() or if $date >= date() but you need to convert it first. Quote Link to comment Share on other sites More sharing options...
PRodgers4284 Posted February 21, 2008 Author Share Posted February 21, 2008 You compare by simply doing if $date >= NOW() or if $date >= date() but you need to convert it first. Thanks for that, appreciate your help, il give that try Quote Link to comment Share on other sites More sharing options...
PRodgers4284 Posted February 21, 2008 Author Share Posted February 21, 2008 I have the following code now, but im getting an error on the line "if $date >= NOW() or if $date >= date()" not sure if i have put this is correct. My code is now //Date check) if (empty($date)) { //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter a dob $date_message = '*Please enter a closing date*'; } //Check the format and explode into $parts elseif (!ereg("^([0-9]{4})/([0-9]{2})/([0-9]{2})$", $date, $parts)){ $error_stat = 1; //Set the message to tell the user the date is invalid $date_message = '*Invalid date, must be YYYY/MM/DD format*'; } elseif (!checkdate($parts[2],$parts[1],$parts[3])) { $error_stat = 1; //Set the message to tell the date is invalid for the month entered $date_message = '*Invalid date, month must be between 1-12*'; } if $date >= NOW() or if $date >= date() { $error_stat = 1; //Set the message to tell the user the date is invalid for the year entered $date_message = '*Invalid date*'; } Quote Link to comment Share on other sites More sharing options...
revraz Posted February 21, 2008 Share Posted February 21, 2008 You took my post literaly, it was merely an example of what you can do. My post was a reference on how to do it, not actual code. Quote Link to comment Share on other sites More sharing options...
PRodgers4284 Posted February 21, 2008 Author Share Posted February 21, 2008 I have the following code for the date check, but im not sure if im doin it correctly: //Date check) if (empty($date)) { //Set the error_stat to 1, which means that an error has occurred $error_stat = 1; //Set the message to tell the user to enter a dob $date_message = '*Please enter job closing date*'; } //Check the format and explode into $parts elseif (!ereg("^([0-9]{4})/([0-9]{2})/([0-9]{2})$", $date, $parts)){ $error_stat = 1; //Set the message to tell the user the date is invalid $date_message = '*Invalid closing date, must be YYYY/MM/DD format*'; } elseif (!checkdate($parts[2],$parts[1],$parts[3])) { $error_stat = 1; //Set the message to tell the date is invalid for the month entered $date_message = '*Invalid date, month must be between 1-12*'; } if ($date <= NOW || $date <= date); { $error_stat = 1; //Set the message to tell the user the date is invalid for the year entered $date_message = '*Invalid date*'; } Quote Link to comment 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.