Perl – days ago subroutine

Here’s a small subroutine, written in Perl, which takes a date, and returns the number of days since this date:

#Pass date as mm/dd/yyyy hh:mm:ss and convert it to a # of days ago
sub days_ago {
 use Time::Local;
 my ($t) = @_;
 my ($date,$time) = split(/ /,$t);
 my ($mon,$day,$year) = split(/\//,$date);
 my ($hour,$min,$sec) = split(/:/,$time);
 my $last = timelocal($sec,$min,$hour,$day,$mon-1,$year);
 my $today = time();
 my $since = sprintf("%d",($today-$last)/(60*60*24));
 #print "<!--Days ago: $since - $sec,$min,$hour,$day,$mon,$year -->\n";
 return($since);
}

Example:

print &days_ago("10/01/2016 08:00:00");

Category: Programming Comments Off on Perl – days ago subroutine

Comments are closed.

Back to top