Simple Time Difference

#!/usr/bin/perl
use Time::Local;

print &daysFromNow('20','NOV','08');

exit 0;

# timegm returns seconds from 1970
# its format is (0,0,0,day 1..31,month 0..11,year)
sub daysFromNow
{
my $date2checkDD = $_[ 0 ];
my $date2checkMMM = $_[ 1 ];
my $date2checkYY = $_[ 2 ];

my $check_day = $date2checkDD;
my $check_month;
if ( $date2checkMMM eq 'JAN' ) { $check_month = 0; }
elsif ( $date2checkMMM eq 'FEB' ) { $check_month = 1; }
elsif ( $date2checkMMM eq 'MAR' ) { $check_month = 2; }
elsif ( $date2checkMMM eq 'APR' ) { $check_month = 3; }
elsif ( $date2checkMMM eq 'MAY' ) { $check_month = 4; }
elsif ( $date2checkMMM eq 'JUN' ) { $check_month = 5; }
elsif ( $date2checkMMM eq 'JUL' ) { $check_month = 6; }
elsif ( $date2checkMMM eq 'AUG' ) { $check_month = 7; }
elsif ( $date2checkMMM eq 'SEP' ) { $check_month = 8; }
elsif ( $date2checkMMM eq 'OCT' ) { $check_month = 9; }
elsif ( $date2checkMMM eq 'NOV' ) { $check_month = 10; }
elsif ( $date2checkMMM eq 'DEC' ) { $check_month = 11; }
my $check_year = "20$date2checkYY";

my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime( time );
$time_1=timegm(0,0,0,$mday,$mon,$year);
$time_2=timegm(0,0,0,$check_day,$check_month,$check_year);

$seconds = $time_2 - $time_1;
$days = $seconds / 86400; # 60*60*24 = 86400
return $days;
}

Leave a Reply