Jump to content


Photo

validating dates entered in form


  • Please log in to reply
2 replies to this topic

#1 ambrennan

ambrennan
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 01 April 2006 - 07:08 PM

I'm new to php and having trouble writing an If statement (at least I think it should be an if statement)which would check whether the user has entered an invalid date for a month and display an error message like "there are only 28 says in February" or 30 days in April. I have been trying the code below which is not working

if ($arrDate_mon = 2, $arrDate_day <28)
{
echo "<h2>There are only 28 days in February</h2>"


#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 01 April 2006 - 07:40 PM

Example:
if (2 == $arrDate_mon && $arrDate_day > 28) {
  echo "<h2>There are only 28 days in February</h2>"
}

However, it's best to check the date entered as a whole because the above doesn't account for leap years where you can have 29 days in February. Try using checkdate() function:

[a href=\"http://us2.php.net/manual/en/function.checkdate.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.checkdate.php[/a]

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 01 April 2006 - 07:41 PM

That is not the correct syntax for an "if" statement. Where did you see that syntax?

It should be something like:
<?php
if ($arrDate_mon == 2 && $arrDate_day > 28) echo "<h2>There are only 28 days in February</h2>";
?>

Of course, this check will not work in leap years.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users