Jump to content

validating dates entered in form


ambrennan

Recommended Posts

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>"
Link to comment
Share on other sites

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

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]
Link to comment
Share on other sites

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

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

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

Ken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.