RE: [PHP] checkdate function
| From: | Jon Haworth | Date: | Tue, 01 Apr 2003 09:17:27 +0000 |
| Subject: | RE: [PHP] checkdate function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-141792@lists.php.net to get a copy of this message | ||
Hi Siva,
> checkdate function verifies whether the date is valid or
> not by taking month, day and year as arguments.
> The problem is when someone enters a three digit year by
> mistake (200 instead of 2003), this function does not catch it.
Yes, I've been bitten by this as well :-)
> We are separating the year part from the string and validating
> separately to solve this problem. Is there a better way to do it?
I think you're stuck with the extra validation step, but you can do it quite
neatly with a replacement function that looks something like this:
function myCheckdate ($m, $d, $y, $min = 1900, $max = 2100)
{
// check whether $y is within allowable range
if ($y <= $min || $y >= $max)
return false;
// the year is OK: checkdate can do its stuff
return (checkdate($m, $d, $y))
}
You can adjust the default allowable years to match what you usually need,
and then override them as necessary.
Personally I'd like to see PHP's checkdate() work a bit like the one above,
or maybe have checkReasonableDate() and checkImprobableDate(): I'm prepared
to believe that some PHP developers need to validate three- and five- digit
years, but I can't believe that it's *that* common :-)
Cheers
Jon
--
Need a web or desktop developer in the London area?
Hire me! http://www.laughing-buddha.net/jon/