Re: Checking input format
| From: | Justin French | Date: | Tue, 29 Apr 2003 11:28:20 +0000 |
| Subject: | Re: Checking input format | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-145632@lists.php.net to get a copy of this message | ||
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