I am not an expert but I have tried to use the method suggested by Kirrily Robert and it just gives the number of days portion, not the total number of days between 2 dates.


The method I use is: subtract_datetime_absolute() method.

for example:
$dt2 and $dt1 are 2 datetime objects with $dt2 greater than $dt1;

my $abs_duration = $dt2->subtract_datetime_absolute($dt1);

$abs_duration holds the number of seconds and nanoseconds between the 2 dates;

to convert to days I simply use this conversion.

my $days = $abs_duration->seconds / (60 * 60 * 24);

for date2 = 2005-05-14 and date1 = 2005-03-14, I get 62 days;

doing the standard way I get 2 months and 1 day

Below is a quote from perldoc datetime.

subtract_datetime_absolute( $datetime )
 This method returns a new "DateTime::Duration" object representing
 the difference between the two dates. The duration object will only
 have deltas for seconds and nanoseconds. This is the only way to
 accurately measure the absolute amount of time between two
 datetimes, since units larger than a second do not represent a fixed
 number of seconds.

----- Original Message ----- From: "Dave Rolsky" <[EMAIL PROTECTED]>
To: "Kirrily Robert" <[EMAIL PROTECTED]>
Cc: <datetime@perl.org>
Sent: Tuesday, April 26, 2005 12:48 AM
Subject: Re: DateTime::Duration and days



On Tue, 26 Apr 2005, Kirrily Robert wrote:

I'm trying to use DateTime::Duration to show the difference in days, as
an integer, between two dates.  For instance, the difference between
March 1st and April 2nd should be 32 days.  Unfortunately if I use the
math stuff in Datetime, I end up with a duration object which says one
month and one day, and I can't for the life of me figure out how to make
it do otherwise.

Is this possible, or am I trying to do something insane?  What *should*
I be doing?

There's a delta_days() method in the DateTime class which can be used to give you a duration with only days.



-dave

/*===================================================
VegGuide.Org www.BookIRead.com
Your guide to all that's veg. My book blog
===================================================*/



Reply via email to