Re: Checking input format

From: Date: Tue, 29 Apr 2003 12:32:41 +0000
Subject: Re: Checking input format
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-145633@lists.php.net to get a copy of this message
If you're using a form to allow the user to input a date (i.e. text box) why not just use three drop downs? One for year, one for day, one for month. You could even use PHP's built in functions to check to make sure that you don't accidentally display a date outside of range. -Dan Anderson On Tue, 2003-04-29 at 07:28, Justin French wrote: > on 29/04/03 10:35 PM, Shaun (shaun@mania.plus.com) wrote: > > > How can i check that a user has input the correct format for a date (i.e. > > yyyy-mm-dd) and that that date has not past? > > Try http://www.php.net/manual/en/function.checkdate.php > > Or, to get more detailed about it, here's some EXAMPLES of wht you might > want to do -- even though it's not efficient code. > > <? > $date = "2002-04-19"; > $validFormat = 1; > > // split it into chunks based on '-' being the separator > list($y,$m,$d) = explode('-',$date); > > // make sure all are numeric > if(!is_numeric($y)) { $validFormat = 0; } > if(!is_numeric($m)) { $validFormat = 0; } > if(!is_numeric($d)) { $validFormat = 0; } > > // make sure lengths are right > if(!strlen($y) != 4) { $validFormat = 0; } > if(!strlen($m) != 2) { $validFormat = 0; } > if(!strlen($d) != 2) { $validFormat = 0; } > > // maybe make sure they are not greater than the max req'd > if($m > 12) { $validFormat = 0; } > if($d > 31) { $validFormat = 0; } > > // convert the $date into a time stamp and compare with today > $dateStamp = strtotime($date); > if($dateStamp > time()) { > // date is in future > } > ?> > > > Really depends what you're aiming for :) > > > Justin > > >

« previous php.general (#145633) next »