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
https://forums.phpfreaks.com/topic/6349-validating-dates-entered-in-form/
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]
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.