Reply to Re: Newbie needs to varify a calendar date

Your name:

Reply:


Posted by larry on 01/27/07 04:33

I hate having to enter dropdown date entries, why not just a text input
field - it will make your users happier.

I prefer to store dates in the YYYYMMDD format

Here is a a couple functions to validate a text date to a numeric
format but can be easily adapted to whatever output format you prefer.


<?php
/* convert (validate) american date string value to a neumeric date
value
** inputs:
** date string in the format of: "mm/dd/yyyy" or "mm-dd-yyyy" or
"mm.dd.yyyy" (even "1/6/92" is ok)
** with 2 or 4 digit years (2 digit numbers above 20 are assumed in
the 1900s)
** or even YYYYMMDD for us programmer types.
** lowest year acceptable - default 1920
** greatest year accepable - default 2300
** return 0 if empty
** result -1 if date is out of range or input found to be invalid
*/

function datestringtonum($input, $lowyear = 1920, $maxyear = 2300 ) {
$sdate = "";
// convert possible YYYYMMDD entry to MM/DD/YYYY string
if($input > 10000000 and $input < 99999999){
$input = datenumtostring($input);
}
// split date based on different seperators
if ( strpos($input,"/") != 0) { // if seperators are '/'s...
$sdate = explode("/",$input);
} else if ( strpos($input,"-") != 0) { //if seperators are '-'s
$sdate = explode("-",$input);
} else if ( strpos($input,".") != 0) { //if sperators are '.'s
$sdate = explode(".",$input);
}
// if array, so far so good.
$ndate = 0;
if( is_array($sdate)){
// 2 digit years to 4 digit years
if ( $sdate[2] < 99 ) { //check/correct for 2 digit year
$sdate[2] = $sdate[2] + ( $sdate[2] < 20 ? 2000 : 1900 );
}
// check if date is a valid calendar date
if (checkdate($sdate[0],$sdate[1],$sdate[2])) {
//check if date falls within year limits
if ( $sdate[2] < $lowyear or $sdate[2] > $maxyear ) { //
make sure date is within 1920 to next 20 years
$ndate = -1;
} else {
// build a number value YYYYMMDD
$ndate =
$sdate[2].sprintf("%02d",$sdate[0]).sprintf("%02d",$sdate[1]);
}
} else {
$ndate = -1;
}
//if not empty, then it's a bad date string.
} elseif (!empty($input)) {
$ndate = -1;
}
return $ndate;
}

/* convert a readable date number YYYYMMDD to a string date
"MM/DD/YYYY"
** input neumeric: YYYYMMDD
** output string: "mm/dd/yyyy"
*/
function datenumtostring($nval) {
if($nval>0){
$dval =
substr($nval,4,2)."/".substr($nval,6,2)."/".substr($nval,0,4);
} else {
$dval = "";
}
return $dval;
}
?>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация