Following script can be used to calculate difference between 2 dates.
sub date_diff {
my %hash = @_;
if($hash{start_day} && $hash{start_day}=~/^\d{1,2}$/ && $hash{start_month} && $hash{start_month}=~/^\d{1,2}$/ && $hash{start_year} && $hash{start_year}=~/^\d{4}$/) {
if(!$hash{end_day} && !$hash{end_month} && !$hash{end_year}) {
$hash{end_day}=strftime "%d", localtime;
$hash{end_month}=strftime "%m", localtime;
$hash{end_year}=strftime "%Y", localtime;
} elsif($hash{end_day} && $hash{end_month} && $hash{end_year} && $hash{end_day}=~/^\d{1,2}$/ && $hash{end_month}=~/^\d{1,2}$/ && $hash{end_year}=~/^\d{4}$/) {
} else {
return 0;
}
use Time::Local;
my @start_sec = (0, 0, 0, $hash{start_day}, int($hash{start_month})-1, $hash{start_year});
my @end_day_sec = (0, 0, 0, $hash{end_day}, int($hash{end_month})-1, $hash{end_year});
my $starttime = timelocal(@start_sec);
my $endtime = timelocal(@end_day_sec);
my %result;
$result{s}=$endtime-$starttime;
$result{i}=ceil($result{s}/60);
$result{h}=ceil($result{s}/60/60);
$result{d}=ceil($result{s}/60/60/24);
$result{m}=ceil($result{s}/2629743);
$result{y}=ceil($result{s}/31556926);
return \%result;
} else {
return 0;
}
}
my $result=&date_diff(start_day=>12, start_month=>6, start_year=>2015,end_day=>13, end_month=>6, end_year=>2015);
print "In seconds :- ".$result->{s};
print "In minutes :- ".$result->{i};
print "In Hours :- ".$result->{h};
print "In days :- ".$result->{d};
print "In months :- ".$result->{m};
print "In years :- ".$result->{y};
Output :-
In seconds :- 86404
In minutes :- 1440
In Hours :- 24
In days :- 1
In months :- 0
In years :- 0
Like this:
Like Loading...
You must be logged in to post a comment.