Article View: adobe.golive.dynamic.content
Article #32Re: Formatting a user input date
From: Jerry_Artman
Date: Fri, 25 Jun 2004 17:46
Date: Fri, 25 Jun 2004 17:46
63 lines
2624 bytes
2624 bytes
Here's the server side check script I use in the validate and forward technique. There are javascript examples on the web for client side checking.
//
// now test date for validity
//
function cvdate($s) {
//this function takes a "human" date and converts it into a UNIX timestamp, zero if error.
//what is the delimiting character? (support space, slash, dash)
$delimiter = "";
if (strpos($s,"-")>0) $delimiter="-";
if (strpos($s,"/")>0) $delimiter="/";
if (strpos($s," ")>0) $delimiter=" ";
if ($delimiter=="") return 0;
//chop it up
$p1=strpos($s,$delimiter);
$p2=strpos($s,$delimiter,$p1+1);
$x=substr($s,0,$p1);
$y=substr($s,$p1+1,$p2-$p1);
$z=substr($s,$p2+1);
//echo("
Debug1--->$x/$y/$z
");
//the last value is always the year, so check it for 2- to 4-digit convertion
if (intval($z)<100){ if (intval($z)>69) $z=strval(1900+intval($z)); else $z=strval(2000+intval($z));
}
//intelligently select which converter to use
//(default is M/D/Y, but if the month is "spelled out" then the format is D/M/Y
return cvdate_numeric($x,$y,$z);
}
//just a helper function
function cvdate_numeric($m,$d,$y) {
$d2=0; $m2=0; $y2=0;
$d2=intval($d);
$m2=intval($m);
$y2=intval($y);
//check for errors!
if (($d2==0)||($m2==0)||($y2==0)) return 0;
$dateTest=mktime(0,0,0,$m2,$d2,$y2);
if(!$dateTest){
// not a valid date
return FALSE;
} else {
// return in mysqlformat YYYYMMDD
return date("Ymd",$dateTest);
}
}
$_POST['censusdate(1)']=cvdate($_POST['censusdate(1)']);
if($_POST['censusdate(1)']){
// date ok
$successMsg.=" The date check passed";
//echo "
".$successMsg."
Final value of censusdate(1)---->". $_POST['censusdate(1)'] ."
";
} else {
// date fails
$errorMsg="ERROR! Date was invalid. Please reenter.";
}
Technical Details
Message-ID:
<3bb4c65d.2@webx.la2eafNXanI>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!npeer.de.kpn-eurorings.net!feed.news.schlund.de!schlund.de!newsfeed01.sul.t-online.de!t-online.de!webxReferences:
<3bb4c65d.-1@webx.la2eafNXanI>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11ffd2,8502de635f9b659d X-Google-Attributes: gid11ffd2,public From: Jerry_Artman@adobeforums.com Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!npeer.de.kpn-eurorings.net!feed.news.schlund.de!schlund.de!newsfeed01.sul.t-online.de!t-online.de!webx Newsgroups: adobe.golive.dynamic.content Subject: Re: Formatting a user input date Message-ID: <3bb4c65d.2@webx.la2eafNXanI> Date: Fri, 25 Jun 2004 10:46:26 -0700 References: <3bb4c65d.-1@webx.la2eafNXanI> Lines: 63 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit