CancelImage Upload

How to validate a date without using regular expressions in PHP

Although regular expressions always look impressive, and are a good way of confusing non-programmers (and some programmers) while coming across as a rocket scientists, there are some instances when regular expressions are indeed not required. And validating a date is one of them.

The main concern of course when validating a date entered by a user is that the day specified is not beyond the amount of days to which the month is limited. The following function, checks that no nonsense dates have been entered (the 30th of February for instance) among other things:

<?php

$day = 17;

$month = 8;

$year = 2009;

echo checkdate($month, $day, $year);

?>

The function will either return 1 (true) if valid, or 0 (false) if not valid. The year has to be between 0 and 32767. Of course for February leap years will be taken into account.

Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com