Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How do I validate a date?

by Anonymous Monk
on May 25, 2000 at 02:35 UTC ( [id://14685]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (dates and times)

I've created a form that has drop downs for the date. like this:

<select name="day" size="1"> <option value="1"> 1</option> <option value="2"> 2</option> <option value="3"> 3</option> .. <option value="31">31</option> </select> <select name="month" size="1"> <option value="1"> 1</option> .. <option value="12">12</option> </select> <select name="year" size="1"> <option value="2000">2000</option> <option value="2001">2001</option> </select>

then in a perlscript I go:

my $post = param('day') . "-" . param('month') . "-" . param('year');
to use in a query against a DB.

Is there a function in perl to make sure someone doesn't choose 31-FEB-2000, or 29-FEB-1999?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I validate a date?
by plaid (Chaplain) on May 25, 2000 at 03:06 UTC
    There are a couple ways to do this. The best that comes to mind is to grab a module for it (I prefer Date::Calc personally). You could then do something like
    unless(check_date(param('year'), param('month'), param('day')) { # do something }
    A non-modular way to accomplish this would be to create an array of month lengths, i.e.
    my @month_lengths = (31, 28, 31, 30, ......);
    which would require one additional check for leap-year.
Re: How do I validate a date?
by ZZamboni (Curate) on May 25, 2000 at 03:00 UTC
    You could use the ParseDate subroutine from Date::Manip (or some other date manipulation package) to parse the date. If it parses correctly, it must be valid.

    But Date::Manip could be overkill if all you want to do is validate the date. You could just check the day of the month against an array indexed by month in which you store how many days each month has. You would have to pre-check for leap years and modify the number of days in february accordingly.

    Leap-year code posted by Adam -- Q&AEditors

    # $year is a leap year if ($year % 4 == 0) and ($year % 100 != 0 or $year % 400 == 0)
    --ZZamboni
Re: How do I validate a date?
by echo (Pilgrim) on Nov 05, 2001 at 20:59 UTC
    use Date::Calc (); sub valid_date($$$) { my ($day, $month, $year) = @_; return Date::Calc::check_date($year, $month, $day); }
Re: How do I validate a date?
by I0 (Priest) on Dec 10, 2000 at 14:39 UTC
    use POSIX; $validdate = (gmtime mktime 0,0,0,$day,$mon-1,$year-1900)[4] == $mon-1 +;
Re: How do I validate a date?
by frankus (Priest) on Nov 05, 2001 at 19:29 UTC
    I wrote this today for a problem I've got, I'm sure the cook book says something similar..HTH :)
    sub valid_date($$$){ my ($day,$month,$year)=@_; $month--; my $epoch=timelocal(0,0,0,$day,$month,$year); my $vdate=localtime($epoch); my @compdate=($vdate->mday,$vdate->mon+1,$vdate->year+1900); for (0..2){print ; if($compdate[$_]!=$_[$_]){ return 0}} 1; }
Re: How do I validate a date?
by BaldPenguin (Friar) on Nov 18, 2005 at 18:14 UTC
    I wanted to validate a date AND it's format, and wanted to allow any ISO standard formatted date. This may not be the most elegant, but it does work.
    use Date::Parse; use Date::Manip qw(); sub is_valid_date_string { my $str = shift; my @elms = strptime($str); # Date::Parse will leave undefs if the elem cannot be found in the + string return 0 unless defined($elms[3]) && defined($elms[4]) && defined( +$elms[5]); my $str_r = sprintf("%4d-%02d-%02d", $elms[5]+1900, $elms[4]+1, $e +lms[3] ); # Run back trhough Date::Manip to make sure whatever we parsed is +a valid date return 0 unless Date::Manip::UnixDate($str_r,"%Y-%m-%d") eq $str_r +; return 1; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://14685]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-03-28 21:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found