use Time::Local qw(timelocal); # This function will return the date in a second from epoch time # Example: # my $timeInSec = getTimeInSec('2005-03-28 12:00:00'); # BUT # actually function work with format '2005-03-28' if you need full date/time you have to make some changes. # If you will have problem with it please contact me. sub getTimeInSec { my $time = shift; my ( $year,$month,$day ); if ( $time =~ /^ (\d\d\d\d) # Parsing Year [-\x20.\/]? # Delimiter (1[0-2]|0?[1-9]) # Parsing Month [-\x20.\/]? # Delimiter (3[0-1]|[1-2][0-9]|0?[1-9]) # Parsing Day $ /x or die "Date format '$time' is wrong" ) { ( $year,$month,$day ) = ( $1,$2,$3 ); die "Date format '$time' is wrong" unless &checkDateFormat( $year,$month,$day ); } return timelocal(00,00,00,$day,$month-=1,$year-=1900); } # This function check if the given string is a correct date. sub checkDateFormat { my ( $year,$month,$day ) = @_; my %month_day = ('01' => 31 ,'03' => 31 ,'02' => &checkLeapYear($year) ,'04' => 30 ,'05' => 31 ,'06' => 30 ,'07' => 31 ,'08' => 31 ,'09' => 30 ,'10' => 31 ,'11' => 30 ,'12' => 31); $month = '0' . $month if length( $month ) == 1; return 1 if ($day <= $month_day{ $month }); return 0; } sub checkLeapYear { return 29 unless $_[0] % 400; return 28 unless $_[0] % 100; return 29 unless $_[0] % 4; return 28; } # To use this function you can write # printf "%04d-%02d-%02d %02d:%02d:%02d", &timenow(); # or # my $dateTime = sprintf "%04d-%02d-%02d %02d:%02d:%02d", &timenow(getTimeInSec('2005-03-28 12:00:00')); sub timenow { my ($sec,$min,$hour,$day,$month,$year) = localtime(defined $_[0] ? $_[0] : time); return $year+1900,$month+1,$day,$hour,$min,$sec; }