RE: [PHP] date validation problem!

2002-10-31 Thread Jon Haworth
Hi,

 I had provided a text field for date, so have to
 expect all the different possible entry, 

This is generally a *very* bad idea. If a visitor to your site enters
4/3/01, do they mean the 4th March or the 3rd April?

I find the best thing to do is provide three drop-downs: one contains
numbers from 1-31, one contains the names of months, and one contains
numbers from 2000 onwards. Using this, it's impossible to enter a date that
could be interpreted in more than one way (although you still can do things
like 31st Feb, so you still need checkdate).

If you absolutely must do this you can always break the date up into
components (split on the delimiter) and pass the components to checkdate().

 When i tried with checkdate() method it needs the
 parametes in (int month,int date, int year). It make
 the code more complex 

Generally it's better to have complex code that works than simple code that
doesn't.

Cheers
Jon

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] date validation problem!

2002-10-31 Thread 1LT John W. Holmes
 I am having some problem with date validation
 I am trying to validate the date in a form Field
 I had provided a text field for date, so have to
 expect all the different possible entry,
 When i tried with checkdate() method it needs the
 parametes in (int month,int date, int year). It make
 the code more complex , What i am looking is some
 thing similar to is_int(), like the one we have in VB
 isdate()
   thanks in advance

There's nothing like that, you have to create it yourself. I personally hate
the three drop down method that someone else suggested, but that's just me.

I prefer to have a text box like you, but tell the user what the accepted
format is. You can then write a simple function to parse their answer and
make sure it's correct. You can also use that function to convert it to a
Unix timestamp or MySQL timestamp.

I prefer to accept my dates in the military format of DDMMM or DDMMMYY
(17NOV75 or 17NOV1975).

I use this function to validate the input:

//check that date matches 17nov75 or 17nov1975 format.
//if it does, returned as mysql format date mmdd
function date($input,$label,$format)
{
$retval = false;

//defaults
$month_array =
array(jan=01,feb=02,mar=03,apr=04,may=05,jun=06
,jul=07,aug=08,sep=09,oct=10,nov=11,dec=12);
$days_in_month = array(31,28,31,30,31,30,31,31,30,31,30,31);

//verify date matches 17nov75 or 17nov1975 format

if(eregi(^([0-9]{1,2})-?(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)-?
((19|20)?[0-9]{2})$,$input,$regs))
{
//convert month to number
$regs[2] = strtolower($regs[2]);
$month = $month_array[$regs[2]];

//make days two digits if it's not already
$day = $regs[1];
if($day  10  strlen($day) == 1) { $day = 0 . $day; }
if($regs[3]  100)
{
//account for 2 digit year
// 30 = 20xx
// 30 = 19xx
if($regs[3]  30) { $year = 20 . $regs[3]; }
else { $year = 19 . $regs[3]; }
}
else { $year = $regs[3]; }

//verify day does not exceed days in month
if(($day = $days_in_month[$month-1]  $day  0) || ($day==29
 $month==2  $year%4==0))
{
$retval = $year.$month.$day;
}
else
{
$this-error[] = $label . : date has invalid day;
}
}
else
{
$this-error[] = $label . : date does not match format of
17NOV75 or 17NOV1975;
}

return $retval;
}


---John Holmes...


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php