POSIX includes a mktime function wich takes (seconds, minutes, hours, day (0 based), month (o based), year (1900 based)) and returns the epoch seconds. So if you use it on each date to get there seconds, then subtract and divide by the number of seconds in a day you get the number of days seperating two days.

use strict; use warnings; my $first = "11/15/2007"; my $second = "12/25/2007"; use POSIX qw/mktime/; sub days_between { my ($start, $end) = @_; my ($m1, $d1, $y1) = split ("/", $start); my ($m2, $d2, $y2) = split ("/", $end); my $diff = mktime(0,0,0, $d2-1, $m2-1, $y2 - 1900) - mktime(0 +,0,0, $d1-1, $m1-1, $y1 - 1900); return $diff / (60*60*24); } print "Between $first and $second there are ", days_between($first, $s +econd), " days\n";

I have no idea if this handles leap anything but I feel pretty safe in assuming that it does ;)


___________
Eric Hodges

In reply to Re: Comparing two dates and finding out number of days by eric256
in thread Comparing two dates and finding out number of days by Ashes.cfg

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.