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?


In reply to Re: Number of days since by FreeBeerReekingMonk
in thread Number of days since [solved] by Skis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.