boo_lolly Posted December 11, 2007 Share Posted December 11, 2007 i am writing a form validation script and i need help with a date field. users will enter in a date from a javascript datePicker in the form of MM/DD/YYYY. i already have regex validation to handle this, however, i need another feature to validate this date. the date entered cannot be a day before the current date. i'm not sure if you can do a less than or greater than with two dates, and how you get the date function to interpret a string date. really i have no idea where to go. can someone help me get started? Quote Link to comment Share on other sites More sharing options...
blackcell Posted December 11, 2007 Share Posted December 11, 2007 Hey, I am fairly new to PHP, but I have completed a few date comparison projects as of late. I usually pick the dates apart into $current_month, $current_day, $current_year variables. I do the same with desired month, date year. Then I use if statements to compare the two and throw errors accordingly. See below: <?PHP //Lets breakdown the Current Date. If you are using a different date structure, modify the substr accordingly. //If you need more help on substr, search PHP substr on google and click the first link. $current_date = date("mdy"); //These three lines pull the year, date and month into seperate variables to work with. $current_m = substr($current_date,0,2); $current_d = substr($current_date,2,2); $current_y = substr($current_date,4,2); //Lets breakdown the Target Date I am replacing the / with nothing because it is less confusing and the numbers //are the same as above. $target_date = "12/20/07"; $target_date = trim(str_replace("/","",$target_date)); $target_m = substr($target_date,0,2); $target_d = substr($target_date,2,2); $target_y = substr($target_date,4,2); //Now your months, days, and years are broken into seperate variables. This can be accomplished with arrays too //but this is easier if your not familiar with arrays. //Lets set some error messages to tell us why we are being rejected! $error_y = "You have selected a year in the past."; $error_m= "You have selected a month in the past."; $error_d = "You have selected a day in the past."; //Lets test to see if the target date is before the current date. if($target_y > $current_y){ if($target_m > $current_m){ if($target_d > $current_d){ echo "SUCCESS! You have selected a date that is not today or before today but in the future!"; }else{ echo $error_d; } }else{ echo $error_m; } }else{ echo $error_y; } ?> Let me know if this helps. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted December 11, 2007 Author Share Posted December 11, 2007 yeah that's what i figured. since i have a javascript datePicker and regex, i can validate the date that is inputted no problem, once the date format has been validated, i can then validate the user inputed date is NOT before the current date like this: <?php $currentdate = date('m/d/Y'); list($cm, $cd, $cy) = split('/', $currentdate); list($um, $ud, $uy) = split('/', $_POST['userinputdate']); if ($um < $cm) { return $error; } if ($ud < $cd) { return $error; } if ($uy < $cy) { return $error; } ?> but as you mentioned in your post, that won't work because a month may be before the current month, but the year may be after, therefore it will be valid. so nested if statements are necessary, but your code is wrong. the logic is a little off (i think). i'm working on it right now. i'll get back when it works. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 11, 2007 Share Posted December 11, 2007 I would just convert it to unix time, do the compare, and then do what you need to do after. Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 11, 2007 Share Posted December 11, 2007 There are a hundred ways you can do this, so it's up to you to pick one and run with it. You can use your date stamp as formatted (MM/DD/YYYY) as the argument in strtotime(), which will result in a Unix timestamp. You can then use date() and mktime() to literally do any comparison you could possibly dream up. I would suggest following those links and becoming educated on how to use those time functions. PhREEEk Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted December 11, 2007 Author Share Posted December 11, 2007 revraz, how do i convert it to unix time, and how do i compare it? that's the information i'm looking for. Quote Link to comment Share on other sites More sharing options...
blackcell Posted December 12, 2007 Share Posted December 12, 2007 strtotime is the method of getting a unix time stamp. PHP Manual has great information on how to use it and pull the string out of a timestamp into regular time. Quote Link to comment Share on other sites More sharing options...
blackcell Posted December 12, 2007 Share Posted December 12, 2007 mktime will get the timestamp for the date and strtotime will convert about anything you can imagine into unix timestamp. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted December 12, 2007 Author Share Posted December 12, 2007 thanks guys i figured it out. it was pretty simple with strtotime and mktime. 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.