Page 1 of 1

verifying date help??

Posted: Sun Dec 08, 2002 10:21 am
by CodeAc
Hi :

I'm trying to take a form field and verify weather the user entered a valid date. I'm the form I have the field defaulted to the current date but the user can enter another date when they submit the form it saves all this information to the database.

any help here would be appretiated.

Thanks in advance.

Posted: Sun Dec 08, 2002 11:11 am
by Bill H
I use drop-down selection boxes for day. month and year.
Then no verification as to validity is required.
It's not 100% perfect, since a user can still enter Nov 31st, but...

Substr

Posted: Mon Dec 09, 2002 4:03 pm
by Rincewind
If you get the date from one field I guess you could run the result trough substr and sort out day, month and year, then validate each of those.

Code: Select all

$date = $_GETї'date'];
// in my country we write a date dd.mm.yy or dd.mm.yyyy
$day = substr($date,0,2); //extract the first two digits ie. the day
$month = substr($date,3,5); //extract the digits from (after) 3 to (including) 5 ie. the month
$year = substr($date,6,10); //extract the digits from (after) 6 to (including) 10 ie. the year
if($day < 32 AND $day > 0 AND $month < 13 AND $month >0 AND $year < 2003 AND $year > 1970 ) // I don't know your criteria here,just an example...
return true;
else
return false;
Of course you can do more checking, you can put the months in groups of 30 and 31 days (february alone of course) and check the day up against the month if it's 31 etc....

Hope this helped

Rincewind_the_Wizzard

Posted: Tue Dec 10, 2002 5:21 am
by CodeAc
Yeah That helped lots. now one question. could I take the 3 variables after a substr and use checkdate on them?

bascially I want to just check if the date is valida and if it it continue proccessing the form but if not display a message to tell the user to re-enter the date.

Thanks again..

checkdate()

Posted: Tue Dec 10, 2002 5:55 am
by Rincewind
You can do this, but there are some pitfalls:
Checkdate takes 3 integers as input in the order month, day, year.
It will return true for 4 4 32000, tough this will not be a good date to work further on, it will accept any value for year between 0 and 32767 and I guess you want more precision than that.
I suggest you first check year the way I showed you, then run checkdate to check if the date is valid for that year.

Code: Select all

if ($year < 2003 AND $year > 1950)
checkdate($month, $day, $year);
Think this should do the trick.

Rincewind_the_Wizzard