Click to See Complete Forum and Search --> : Date validationand Booleans


gizmo
10-29-2003, 10:18 AM
I have a form with a box for the user to enter the date in the format dd/mm/yyyy. The latter appears in the box by default. I have a flag called $valid which is only true if the user replaces the default with numbers. The code I use works ok giving the day of the week also, but I'm sure from my knowledge of other languages that there is a more elegant solution. The flag is also required at a later stage.
$yyyy=substr($date,-4);
$mm=substr($date, 3, 2);
$dd=substr($date, 0, 2);

if (is_numeric($dd) and is_numeric($mm) and is_numeric($yyyy)) {
$valid=true ;
} else {
$valid=false ;
}
if ($valid) {
$date=date ("l d-M-Y", mktime (0,0,0,$mm,$dd,$yyyy)); // formats & o/ps date
}

pyro
10-29-2003, 10:55 AM
This, perhaps?

<?PHP
$date = "10/29/2003";
list ($month, $day, $year) = preg_split("/[\\/-]/", $date);
$valid = false;
if (is_numeric($day) && is_numeric($month) && is_numeric($year)) { #could do a better validation with regexp, if you wanted to
$valid=true;
}
if ($valid) {
$date= date ("l d-M-Y", mktime (0,0,0,$month,$day,$year)); // formats & o/ps date
}
echo $date;
?>

gizmo
10-29-2003, 11:04 AM
Looks good to me. Not sure I understand the line starting with list, but will consult the manual. Thanks, Pyro your a pal:)

pyro
10-29-2003, 11:12 AM
Sure thing. And once you look at the manual, you'll know exactly what it does:

http://us2.php.net/manual/en/function.list.php
list -- Assign variables as if they were an array