verifying date help??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CodeAc
Forum Newbie
Posts: 2
Joined: Sun Dec 08, 2002 10:21 am
Location: Pawtucket, RI

verifying date help??

Post 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.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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...
Rincewind
Forum Commoner
Posts: 27
Joined: Thu Nov 21, 2002 11:15 am
Location: Norway

Substr

Post 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
CodeAc
Forum Newbie
Posts: 2
Joined: Sun Dec 08, 2002 10:21 am
Location: Pawtucket, RI

Post 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..
Rincewind
Forum Commoner
Posts: 27
Joined: Thu Nov 21, 2002 11:15 am
Location: Norway

checkdate()

Post 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
Post Reply