Forum Moderators: coopster

Message Too Old, No Replies

date validation to exclude today and past dates

validation done, just need help with the extra parts

         

php4U

6:53 am on Mar 7, 2008 (gmt 0)

10+ Year Member



I have a form with an input for a date. The validation says that it must be in "mm/dd/yyyy" format, and it checks for 10 total characters.

After testing this I noticed that it actually checks it as dd/mm/yyyy because I can input 03/13/2008 and it prompts to "Enter a Valid Month"

1. I want to see if anyone has any suggestions on getting it into the mm/dd/yyyy format, as the code seems little like it could possibly be shortened up.

2. This is being used for scheduling, so I only want the user to be able to select dates in the future NOT including the current day. Not sure how to check the inputted date against the current date.

<?php $nextday = mktime(0,0,0,date("m"),date("d")+1,date("Y")); //will create a day after the current one
echo "Next Day: ".date("m/d/Y", $nextday); ?>

====================
//Check the length of the entered Date value
if((strlen($strdate)<10)OR(strlen($strdate)>10)){
echo("Enter the date in 'mm/dd/yyyy' format");
}
else{

//The entered value is checked for proper Date format
if((substr_count($strdate,"/"))<>2){
echo("Enter the date in 'mm/dd/yyyy' format");
}
else{
$pos=strpos($strdate,"/");
$date=substr($strdate,0,($pos));
$result=ereg("^[0-9]+$",$date,$trashed);
if(!($result)){echo "Enter a Valid Date";}
else{
//if(($date<=0)OR($date>31)){echo "Enter a Valid Date";} // original code line
if(($date<=0)OR($date>31)OR($date<$nextday)){echo "Enter a Valid Date or one in the future";}
}
$month=substr($strdate,($pos+1),($pos));
//if(($month<=0)OR($month>12)){echo "Enter a Valid Month";} // original code line
if(($month<=0)OR($month>12)OR($month<$nextday)){echo "Enter a Valid Month or one in the future";}
else{
$result=ereg("^[0-9]+$",$month,$trashed);
if(!($result)){echo "Enter a Valid Month";}
}
$year=substr($strdate,($pos+4),strlen($strdate));
$result=ereg("^[0-9]+$",$year,$trashed);
if(!($result)){echo "Enter a Valid year";}
else{
if(($year<date("Y"))OR($year>2200)){echo "Enter a year between 2008-2200";}
}
}
}
DisplayForm();
}

//User-defined Function to display the form in case of Error
function DisplayForm(){
global $strdate;
===========================

Long story short, I did have a javascript popup calendar in place with an "onKeyDown" event handler that only allowed future dates to be picked...but javascript could be turned off in the users browser, or quirks may prevent it from working in certain browsers correctly. More likely than not javascript will be active in the browser, so another possiblity would be to check the selected date with PHP in case the user finds a workaround and trys to change the selected date to the current day. I appreciate any help...as I have a decent start, but am having problems. Thank you.

cameraman

8:36 am on Mar 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To work with dates I find it easier to get them into timestamps as soon as possible.
This line will do two things for you - it will check the input for xx/xx/#*$!x or xx-xx-#*$!x format and will put the numbers into an array (don't forget to fix the broken pipes):
if(!preg_match("#(\d\d)[/¦-](\d\d)[/¦-](\d{4})#",$strdate,$parts))
echo "date must be mm/dd/yyyy";

Now the first number is in $parts[1], the second in $parts[2], and the third in $parts[3].
You can use checkdate() [php.net] to see if it's a valid date.
To make sure it's greater than today, you can use
$tom = strtotime("tomorrow midnight");
to generate a time stamp for tomorrow, and mktime() with the entered components in the array, then just make sure the entered one is >= $tom.

php4U

3:29 am on Mar 8, 2008 (gmt 0)

10+ Year Member



Thank you cameraman,

It looks like I was going in the right direction with the mktime() function, because I created the var $nextday. I will look into the check date function you suggested to see what I can come up with.