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

I am creating a program whis has an input of begin and end date. Im just wondering if theres an easy way in calculating the number of days between those dates or in short words I wanted to get the duration for that particular transaction. Thanks in Advance, Roel.t

Replies are listed 'Best First'.
Re: date subtraction
by borisz (Canon) on Jul 05, 2004 at 08:35 UTC
    Use a module like Date::Calc and the Delta_Days method. Or Date::Manip. Or use HTTP::Date to translate the date to seconds subtract them and divide by seconds of a day.
    use HTTP::Date; print ( str2time($date1) - str2time($date2) ) / 3600;
    Boris
Re: date subtraction
by edan (Curate) on Jul 05, 2004 at 08:38 UTC

    The most commonly recommended Date/Time modules are:

    --
    edan

Re: date subtraction
by neeraj (Scribe) on Jul 05, 2004 at 08:54 UTC
    Use Date::Calc module
    use Date::Calc qw(Delta_Days); $days = Delta_Days( $year1, $month1, $day1, $year2, $month2, $day2);
    or if you have the time also use this form:-
    use Date::Calc qw(Delta_DHMS); ($days, $hours, $minutes, $seconds) = Delta_DHMS( $year1, $month1, $day1, $hour1, $minute1, $seconds1, # +earlier $year2, $month2, $day2, $hour2, $minute2, $seconds2); # +later
Re: date subtraction
by Happy-the-monk (Canon) on Jul 05, 2004 at 08:28 UTC

    There is. Have a look at the   Date   and   Time   modules on   CPAN.

    I could have told you more, had you given me an example.

    Update:
    You find general examples in the Tutorials section of this site.

    Pity me, no, there aren't any tutorials on the   Date   and   Time   modules (yet). Hint, hint...

    Cheers, Sören

Re: date subtraction
by tomhukins (Curate) on Jul 05, 2004 at 17:10 UTC

    Perl's DateTime project aims to produce a comprehensive suite of modules for dealing with dates and times. Historically, such modules in Perl often perform well at certain tasks, but badly at others.

    DateTime overloads the addition and subtraction operators, so you can find the difference between two dates with $duration = $end - $start, where $end and $start are DateTime objects and $duration is a DateTime::Duration object.