Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date verification technique?

Status
Not open for further replies.

msc0tt

IS-IT--Management
Jun 25, 2002
281
CA
Hello,
I need a way to verify a user-entered date. I briefly worked with timelocal(), but discovered that it quite happily returns a value for Feb. 31st. It's parameter checking is quite primative.
Is there a 'clean' way to do this?
-with thanks,
Mike
 
Well, I put on my programmer's hat and came up with something!

My function looks like: &dateValid($mon, $day, $year) where mon is 1..12, day is 1..31, and year is 1970..2100. It returns the logical value TRUE or FALSE.
Comments are welcome, particularily if I've got a logic BUG in this baby! -thanks.


use Time::Local;

sub dateValid {
my ($mm, $dd, $yy) = @_;
my ($FALSE) = 0;
my (@t);

return ($FALSE) if (($mm < 1) || ($mm > 12) || ($dd < 1) || ($dd > 31) || ($yy < 1970) || ($yy > 2100));
$mm--; $yy -= 1900; # convert for use by Perl functions
@t = localtime(timelocal(0, 0, 0, $dd, $mm, $yy));
return (($t[3] == $dd) && ($t[4] == $mm) && ($t[5] == $yy));
}
 
Here is the right logic to validate a date in a given window. Please check the syntax.

Here I choose [1950,2050[: e.g Check a valid date between 1950 and 2050

....


bool dateValid {
my ($day $month, $year) = @_;
if ($year < 50 )
$year+= 2000;
else if ($year <= 99)
$year+= 1900;
if ( $year % 400 == 0 || ($year %100 != 0 && $year % 4 == 0) )
{
if ($month == 2 )
$lastdayinmonth = 29;
else
$lastdayinmonth = 28;
}
if ($month == 4 || $month == 6|| $month == 9 || $month == 11)
$lastdayinmonth = 30;
else
$lastdayinmonth = 31;
if ($day == 0 || $day > $lastdayinmonth)
$bValid = 0;
if (month == 0 || month > 12 )
$bValid = 0;
return $bValid;
}
-obislavu-
 
Date::Manip will also check the validity of a date for you.

Its a bit on the heavy side but does a solid job.
 
I did this in PHP.

I stripped it from a new Date Class I building.

I know you can convert it...

function ckDate( $_intMonth, $_intDay, $_intYear )
{
return ( ( ( $_intYear >= 1 ) && ( $_intYear <= 32767 ) ) &&
( ( $_intMonth >= 1 ) && ( $_intMonth <= 12 ) ) &&
( ( $_intDay >= 1 ) && ( $_intDay <= getDaysInMonth ($_intMonth, $_intYear) ) )
);
}

function getDaysInMonth( $_intMonth, $_intYear )
{
/**
* Array containing days in each month.
*
* @var private array $_aryDaysInMonth
*
**/
$_aryDaysInMonth = array
( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );

if ( ( $_intMonth == 2 ) && isLeapYear ($_intYear) )
$_len = 29;
else
$_len = $_aryDaysInMonth [ ($_intMonth * 1 ) - 1 ];

return $_len;
}

function isLeapYear( $_intYear )
{
return ( ( ($_intYear % 4) == 0 ) &&
( ( ($_intYear % 100) != 0 ) ||
( ($_intYear % 400) == 0 )
)
);
}

 
Hi, If you load the Date::Calc module you can use the check_date function.

if (check_date($year,$month,$day))

This function returns &quot;true&quot; (&quot;1&quot;) if the given three
numerical values &quot;$year&quot;, &quot;$month&quot; and &quot;$day&quot; constitute a valid date, and &quot;false&quot; (&quot;0&quot;) otherwise.

 
snowbee is right, but...

It only works for dates between 1970 and 2038.

This will work for any date.

Thats why I made it.

I needed date methods to work outside the &quot;normal&quot; date system EPOCH range.

Walter
 
Here's what I use...
[tt]
@DAYSINMONTH = (
[ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ],
[ 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
);

sub leap_year {
my ($year) = @_;
my $yy = int ($year/100);
if ($year > 0) {
return ((($year & 0x03) ==0) &&
(( $yy * 100 != $year) ||
(($yy & 0x03) == 0) ) ) ? 1 : 0;
} else {
return '';
}
}

sub check_date {
my ($day, $month, $year) = @_;
my $leap = leap_year($year);
return (($year >= 1) &&
($month >= 1) && ($month <= 12) &&
($day >= 1) &&
($day <= $DAYSINMONTH[$leap][$month])) ? $leap : -1;
}
[/tt]

-- Chris Hunt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top