View Single Post
June 7th, 2007, 04:57 AM
 friendlyhorizon Registered User Join Date: Jun 2007 Location: , , . Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts

This below prog is to calculate the diff of days.
I hardcoded most. if you want to use dd-mm-yy format you can use split function for date arrays.

#!/usr/bin/perl
# perl binary

@std_date=(1,1,1800); # cal starts from this year

@date_one=(21,06,2006); # first date
@date_two=(20,06,2007); # second date

@days_of_month=(31,28,31,30,31,30,31,31,30,31,30,3 1);

\$count1=no_day(@std_date,@date_one); # date_one - std_date
\$count2=no_day(@std_date,@date_two); # date_two - std_date
\$count=(\$count1 > \$count2) ? \$count1 - \$count2 : \$count2 - \$count1;
# date1 - date2

print "\n\tNo of days : \$count\n\n";

sub no_day
{
@std=(\$_[0],\$_[1],\$_[2]);
@date=(\$_[3],\$_[4],\$_[5]);
\$count=0;

for(\$i=\$std[2];\$i<\$date[2];\$i++)
{
\$count+=365 if(leapyear(\$i));
\$count+=366 if(!(leapyear(\$i)));
}

for(\$i=@std[1];\$i<\$date[1];\$i++)
{
\$count+=\$days_of_month[\$i-1];
}

return(\$count+=\$date[0]);

}

sub leapyear # is it leap year
{
\$in=\$_[0];
if(!(\$in%4))
{
if(\$in%100)
{
return(0); # if leap year
}
else
{
if(!(\$in%400))
{
return(0);
}
return(1)
}
}
return(1); # if it is not leap year
}