Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I have to find the number of days remaining between 2 dates.

The dates are in the format

dd-mm-yyyy

into days

This is for an online auction so that I can tell people how many days are remaining until the item expires.

Many thanks,
Tom

Replies are listed 'Best First'.
Re: Converting dates
by gmax (Abbot) on Feb 04, 2003 at 23:44 UTC
Re: Converting dates
by data64 (Chaplain) on Feb 04, 2003 at 23:46 UTC

    try Date::Calc module. Specifically the Delta_DHMS method.

    This is also answered in the Perl FAQ.


    Just a tongue-tied, twisted, earth-bound misfit. -- Pink Floyd

Re: Converting dates
by bart (Canon) on Feb 05, 2003 at 00:21 UTC
    There's a standard module (as in "comes with perl"), Time::Local, which provides inverse functions for localtime and gmtime: timelocal() and timegm(). These can convert dates/times in a list format, (ss, mm, hh, dd, MM, yyyy), to an epoch time. As explained in How do I find the difference in days between two dates, in a cool perl way? which was pointed to in another reply, this is a number of seconds since, typically, January 1st 1970, 00:00:00 GMT. That is the case on Unix and Windows, at least.

    So first you must get your year/month/day out of the string:

    $date = '05-02-2003'; my($d,$m,$y) = $date =~ /(\d+)-(\d+)-(\d+)/;
    Subtract 1 from the month, as January is month 0. Seconds since the epoch can be found like this:
    use Time::Local; my $time = timegm(0, 0, 0, $d, $m-1, $y);
    I use midnight, and GMT, as a reference, with regards to summer time issues — GMT doesn't have them. If you desire finer results, with an exact hours/minutes/seconds count, you can always use timelocal(), and real times.

    The calculated value is 1044403200. You can see that it works well, like this:

    print scalar gmtime($time);
    which produces
    Wed Feb 5 00:00:00 2003
    OK?
Re: Converting dates
by tachyon (Chancellor) on Feb 05, 2003 at 01:21 UTC

    You may find these snippets useful

    $d1 = '05-02-2003 10:30:44'; $d2 = '06-02-2003 11:31:47'; my ( $days, $hours, $mins, $secs ) = delta_date( $d1, $d2 );; die "Auction expired" unless defined $secs; print format_time_remaining( $days, $hours, $mins, $secs), 'remaining' +; sub delta_date { use Time::Local; my ($start, $end) = @_; my($d,$m,$y,$h,$min,$s) = split /[- :]/, $start; my $start_sec = timegm($s, $min, $h, $d, $m-1, $y); ($d,$m,$y,$h,$min,$s) = split /[- :]/, $end; my $end_sec = timegm($s, $min, $h, $d, $m-1, $y); my $diff = $end_sec - $start_sec; return undef if $diff < 0; my $days = int( $diff / (24*3600) ); $diff -= $days * 24*3600; my $hours = int( $diff / (3600) ); $diff -= $hours * 24*60; my $mins = int( $diff/ 60 ); $diff -= $mins * 60; return $days, $hours, $mins, $diff; } sub format_time_remaining { ($days, $hours, $mins, $secs) = @_; my $str = ''; $str .= $days . ' day' . (($days ==1 ) ? ', ' : 's, '); $str .= $hours . ' hour' . (($hours == 1) ? ', ' : 's, '); $str .= $mins . ' minute' . (($mins == 1) ? ', ' : 's, '); $str .= $secs . ' second' . (($secs == 1) ? ' ' : 's '); return $str; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Converting dates
by mojotoad (Monsignor) on Feb 05, 2003 at 06:27 UTC