Validating Dates


i've 'written' simple function validate date; see below ....

function valid_date($date) // validate date in dd/mm/yyyy format
{
$date_element = split("/",$date); // split input date down constituent parts
$dd = $date_element[0]; // first element of array day
$mm = $date_element[1]; // second element month
$yyyy = $date_element[2]; // third element year

if(!checkdate($mm,$dd,$yyyy))
{ return false; }
else
{ return true; }
} // end of date validation function

this partially works returns true if enter before 1901 , e.g. 01/01/1881 thought wouldn't. if enter date alph characters (e.g. i/2/1982) ... "warning: checkdate() expects parameter 1 long, string given in .... " appears on screen out side of control @ least says date false.

i know you're going roll eyes swear routine validated both of these scenarios correctly before today ... unlikey sounds? have manually code these scenarios in 'long hand'?

all best.
patrick.

.oo(patricktr)

>i've 'written' simple function validate date; see below ....
>
> function valid_date($date) // validate date in dd/mm/yyyy format
> {
> $date_element = split("/",$date); // split input date down
>constituent parts
> $dd = $date_element[0]; // first element of array day
> $mm = $date_element[1]; // second element month
> $yyyy = $date_element[2]; // third element year
>
> if(!checkdate($mm,$dd,$yyyy))
> { return false; }
> else
> { return true; }
> } // end of date validation function
>
> partially works returns true if enter before 1901 , e.g. 01/01/1881
>which thought wouldn't.

checkdate() doesn't use unix timestamps, limited 1901-2038,
so result correct - 01/01/1881 valid gregorian date (even
though in ambiguous format).

>also if enter date alph characters (e.g.
>i/2/1982) ... "warning: checkdate() expects parameter 1 long,
>string given in .... " appears on screen out side of control
>at least says date false.

of course can control configuration directives.
on development machine must set in php.ini:

error_reporting = e_all|e_strict
display_errors = 1

of course on production server display_errors should turned off.
you can change there ini_set() @ beginning of script.

besides warning indicates bug in script,
you should fix adding checks before pass values
checkdate(), e.g.

function valid_date($date) {
list($dd, $mm, $yyyy) = explode('/', $date);
if (is_numeric($dd) && is_numeric($mm) && is_numeric($yyyy)) {
return checkdate($mm, $dd, $yyyy);
} else {
return false;
}
}

another possible way check , split $date string use
regular expression , preg_match().

hth
micha


More discussions in Develop server-side applications in Dreamweaver


adobe

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support