On Thu, 2010-07-01 at 23:56 +0200, Peter Lind wrote: > On 1 July 2010 23:50, Ashley Sheridan <ash@xxxxxxxxxxxxxxxxxxxx> wrote: > > > > On Thu, 2010-07-01 at 23:48 +0200, Peter Lind wrote: > > > > On 1 July 2010 23:40, Ashley Sheridan <ash@xxxxxxxxxxxxxxxxxxxx> wrote: > > > On Thu, 2010-07-01 at 14:26 -0700, Don Wieland wrote: > > > > > >> In one of my forms, I am building a variable that I can use as an > > >> INSERT string. > > >> > > >> On my form, I have several DATE fields which exist of 3 fields MM - DD > > >> - YYYY > > >> > > >> when I build my string it looks like this: > > >> > > >> array('dbf'=>'applicant_dob', > > >> 'f'=>array('applicant_dob_1','applicant_dob_2','applicant_dob_3'), > > >> 'req'=>0, 's'=>'/'), > > >> > > >> This enters in the DB fine when there is a DATE, but when these fields > > >> are left empty, it inserts into the the DB as 2069-12-31. > > >> > > >> How does one deal with this? > > >> > > >> Don Wieland > > >> > > > > > > > > > How are you allowing the user to select a date? If it is three form > > > fields, it might be logical to use select lists with the allowed range > > > of values in. > > > > > > If that's not possible, use lines like this to set default values: > > > > > > $month = (isset($_POST['month']) && preg_match('/^\d{2}$/', > > > $_POST['month']))?$_POST['month']:'01'; > > > > > > which would set a default month value of '01' if there was either no > > > month value sent or it wasn't a 2-digit value. > > > > > > > Not sure I'd bother with a preg there, intval should do it nicely. > > Keep in mind you probably want to check the date at the end of this > > with http://dk2.php.net/manual/en/function.checkdate.php or something > > similar. > > > > Regards > > Peter > > > > > > I thought of using intval(). but it would convert the string '01' into the number 1, which wouldn't work in a MySQL date string without the extra padding of a 0. You can do that with sprintf, but then it comes down to what you prefer, intval and sprintf, or a single preg_match call? Not sure there's any cause for concern over speed on this, as any difference would be extremely slight. > > > > The single preg_match will check the format (though not the contents, > so you're stuck with more function calls anyway) but I'd much rather > make sure the data is valid and format the date as I see fit with a > date() call - you never know what users throw at you, and instead of > failing (silently in this case) because you received 1 instead 01 I'd > rather format the data myself. > > Regards > Peter > > -- > <hype> > WWW: http://plphp.dk / http://plind.dk > LinkedIn: http://www.linkedin.com/in/plind > BeWelcome/Couchsurfing: Fake51 > Twitter: http://twitter.com/kafe15 > </hype> I suppose, I only ever check for valid content, not logical content. I let the DB handle the date (I believe if the range goes beyond a proper date it falls into the next date like PHP does), but I tend to use a Javascript picker for the date. The only time I've ever worked with date fields that allow a user entry has been on CMS systems, so I knew in advance what specs the users had (so I could get away with relying on a Javascript input method and hope nothing went wrong!) However, checking to ensure the date is indeed valid and not something like 31st February is a good idea I agree! :) Thanks, Ash http://www.ashleysheridan.co.uk