Date::Calc has a check_date function. From the docs:
if (check_date($year,$month,$day))
This function returns ``true'' (``1'') if the given three numerical values ``$year'', ``$month'' and ``$day'' constitute a valid date, and ``false'' (``0'') otherwise.
For example:
use strict;
use warnings;
use Date::Calc qw( check_date );
my ( $year, $month, $day ) = ( 2006, 1, 30 );
printf "[$year, $month, $day] %s a valid date\n",
check_date( $year, $month, $day ) ? 'is' : 'is not';
( $year, $month, $day ) = ( 2006, 1, 32 );
printf "[$year, $month, $day] %s a valid date\n",
check_date( $year, $month, $day ) ? 'is' : 'is not';
( $year, $month, $day ) = ( 2006, 2, 29 );
printf "[$year, $month, $day] %s a valid date\n",
check_date( $year, $month, $day ) ? 'is' : 'is not';
This prints:
[2006, 1, 30] is a valid date
[2006, 1, 32] is not a valid date
[2006, 2, 29] is not a valid date
|