You are here: Re: Date field in DD/MM/YYY « PHP Programming Language « IT news, forums, messages
Re: Date field in DD/MM/YYY

Posted by larry on 10/27/07 17:33

I prefer to store my dates in YYYYMMDD format on my tables for
compactness and readability, and worked up a bunch of functions for
working with them, which also includes validating mm/dd/yyyy input
(data entry people hate popup lists for date entry).


// days difference between dates input is in YYYYMMDD format.
function daysbetweendatenums($startdatenum, $enddatenum) {
$jstart = gregoriantojd(
substr($startdatenum,4,2)
,substr($startdatenum,6,2)
,substr($startdatenum,0,4));
$jend = gregoriantojd(
substr($enddatenum,4,2)
,substr($enddatenum,6,2)
,substr($enddatenum,0,4));
return intval($jend - $jstart);
}


// add or subtract days/months/years from date to create new date
// Input YYYYMMDD and the days,months,years to add or subtract
function datenumadjust($datenum, $days = 0, $months = 0, $years = 0) {
$newdate = date("Ymd",mktime(0,0,0
,substr($datenum,4,2)+$months
,substr($datenum,6,2)+$days
,substr($datenum,0,4)+$years));
return intval($newdate);
}


/* convert a readable date number "YYYYMMDD" to a DATE() compatible
value */
function datenumtounix($dval) {
$dval=mktime(0,0,0,substr($dval,4,2),substr($dval,
6,2),substr($dval,0,4));
return $dval;
}

/* 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;
}

/* 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"
** 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 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 acceptable range
$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;
}

 

Navigation:

[Reply to this 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

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