Well, there are many libraries that do that, so read up on those from fellow monks.
The reason for that is that there are extra seconds, so some days exist that have 23:59:62 as a real, existing time.
Then, you have a difference between localtime and UTC time.
If you do not care much about that precision, and assume localtime, then you can use timelocal to transform the start and end dates into an epoch (seconds sinds 1970-01-01), substract these two numbers, and divide this number /60/60/24 to get aproximate days, then maybe round the value. Here is an example:
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
my $seconds = 0;
my $minutes = 0;
my $hours = 0;
my $day = 14;
my $month = 7;
my $year = 2016;
my $epoch_from = timelocal($seconds||0, $minutes||0, $hours||0, $day,
+$month-1, $year-1900);
my $epoch_target = timelocal(0, 0, 0, 1, 8-1, 2016-1900);
print secToTimeString0( $epoch_target - $epoch_from );
# This will give you an estimate of time. Used with -'"%;"'
sub secToTimeString0 {
my($t) = @_;
my $s= ($t<0 && ($t*=-1))? '-':'';
# return sprintf("$s%2d seconds", $t) if ($t < 60);
$t /=60; #return sprintf("%s%2.2f minutes", $s, $t) if ($t < 60);
$t /=60; #return sprintf("%s%2.2f hours", $s, $t) if ($t < 24);
$t /=24; return sprintf("%s%2.2f days", $s, $t) ;#if ($t < 30);
# $t /=30; return sprintf("%s%2.2f months", $s, $t) if ($t < 12);
# $t /=12; return sprintf("%s%2.2f years", $s, $t);
}
Instead of the secToTimeString0() function, you can also use round to get an integer number of days, like so:
my $days = int(0.5 + ( $epoch_target - $epoch_from ) /60/60/24 );
How do I round a number?
|