From: David Mehler on
Hello,
I've got a form with three fields that are not required for proper
completion of it, ending month, day, year fields. If a user enters
nothing no problem, but if those form fields are entered I need them
validated. They have to be in the correct format YYYY-MM-DD date
format and that value also has to be greater than the starting date
validated previously. I've got some not working code.
Thanks.
Dave.

// if an ending date field was entered validate that
// also must be greater than the starting date values
if(!empty($_POST['month1']) && !empty($_POST['day1']) &&
!empty($_POST['year1']) {
$month1=$_POST['month1'];
$day1=$_POST['day1'];
$year1=$_POST['year1'];
$date_value1="$year1-$month1-$day1";
}
if(!checkdate($month1,$day1,$year1)) {
echo "Invalid Date.\n";
} else {
echo "Entered Date is correct.\n";
}
}
if(!checkdate($month1,$day1,$year1)) > $date_value {
echo "Invalid Date.\n";
} else {
echo "Entered Date is correct.\n";
}
From: Ashley Sheridan on
On Mon, 2010-06-07 at 16:51 -0400, David Mehler wrote:

> Hello,
> I've got a form with three fields that are not required for proper
> completion of it, ending month, day, year fields. If a user enters
> nothing no problem, but if those form fields are entered I need them
> validated. They have to be in the correct format YYYY-MM-DD date
> format and that value also has to be greater than the starting date
> validated previously. I've got some not working code.
> Thanks.
> Dave.
>
> // if an ending date field was entered validate that
> // also must be greater than the starting date values
> if(!empty($_POST['month1']) && !empty($_POST['day1']) &&
> !empty($_POST['year1']) {
> $month1=$_POST['month1'];
> $day1=$_POST['day1'];
> $year1=$_POST['year1'];
> $date_value1="$year1-$month1-$day1";
> }
> if(!checkdate($month1,$day1,$year1)) {
> echo "Invalid Date.\n";
> } else {
> echo "Entered Date is correct.\n";
> }
> }
> if(!checkdate($month1,$day1,$year1)) > $date_value {
> echo "Invalid Date.\n";
> } else {
> echo "Entered Date is correct.\n";
> }
>


strtotime() can create a timestamp from a date string that you can use
to compare two dates, and the strings can be put together with the
values from the variables. I'm assuming that your form is using a
combination of select lists to create the date, so you should be able to
just grab the integer values to test they are within valid ranges for a
date.

Thanks,
Ash
http://www.ashleysheridan.co.uk


From: Jim Lucas on
David Mehler wrote:
> Hello,
> I've got a form with three fields that are not required for proper
> completion of it, ending month, day, year fields. If a user enters
> nothing no problem, but if those form fields are entered I need them
> validated. They have to be in the correct format YYYY-MM-DD date
> format and that value also has to be greater than the starting date
> validated previously. I've got some not working code.
> Thanks.
> Dave.
>
> // if an ending date field was entered validate that
> // also must be greater than the starting date values
> if(!empty($_POST['month1']) && !empty($_POST['day1']) &&
> !empty($_POST['year1']) {
> $month1=$_POST['month1'];
> $day1=$_POST['day1'];
> $year1=$_POST['year1'];
> $date_value1="$year1-$month1-$day1";
> }
> if(!checkdate($month1,$day1,$year1)) {
> echo "Invalid Date.\n";
> } else {
> echo "Entered Date is correct.\n";
> }
> }
> if(!checkdate($month1,$day1,$year1)) > $date_value {
> echo "Invalid Date.\n";
> } else {
> echo "Entered Date is correct.\n";
> }
>

Well, I don't know if they are cut/paste errors, but you have a few syntax
errors in the above code...

# Initialize your date container variables

$date_value_ts = $date_value1_ts = null;


# Setup start date stuff
if ( !empty($_POST['month']) &&
!empty($_POST['day']) &&
!empty($_POST['year']) )
{
$month = (int)$_POST['month'];
$day = (int)$_POST['day'];
$year = (int)$_POST['year'];
$date_value = "{$year}-{$month}-{$day}";


#if ( !checkdate($month, $day, $year) )
if ( ($date_value_ts = strtotime($date_value) ) === FALSE )
{
echo "Invalid Date.\n";
} else {
echo "Entered Date is correct.\n";
}

}



Test for
if ( !empty($_POST['month1']) &&
!empty($_POST['day1']) &&
!empty($_POST['year1']) )
{
$month1 = (int)$_POST['month1'];
$day1 = (int)$_POST['day1'];
$year1 = (int)$_POST['year1'];
$date_value1= "{$year1}-{$month1}-{$day1}";

#if ( !checkdate($month1, $day1, $year1) )
if ( ($date_value1_ts = strtotime($date_value1) ) === FALSE )
{
echo "Invalid Date.\n";
} else {
echo "Entered Date is correct.\n";
}

}

# Compare the two dates. Make sure end date is after start date

if ( !is_null($date_value1_ts) &&
( (int)$date_value_ts < (int)$date_value1_ts ) )
{
echo "Invalid Date.\n";
} else {
echo "Entered Date is correct.\n";
}


--
Jim Lucas

A: Maybe because some people are too annoyed by top-posting.
Q: Why do I not get an answer to my question(s)?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
 | 
Pages: 1
Prev: Battle of Spam
Next: Finding a font.