I currently use the method subtract_datetime to determine the remaining time available. In some cases the results returned are correct. In some cases the results returned are way off.

Below is how it is being used.

<p>my $endDate = DateTime->new(year => 2015, month => 02, day => 24, t +ime_zone => 'America/New_York');
# the DateTime object, $endDate, normally comes from a database. But I created a DateTime object for this example. #

<p>my $dt1 = DateTime->now( 'time_zone' => 'America/New_York' ); my $duration_obj = $endDate->subtract_datetime($dt1) if $user_obj;</p> <p>print "Expires in: " . $duration_obj->months() . " months, " . $dur +ation_obj->weeks() . " weeks, " . $duration_obj->days() . " days";</p +>

As I indicated, sometimes the results are correct and sometimes it isn't. Is there something I am doing wrong? Or is there another method I can use with DateTime to return consistent results?

***EDITED***

So, if $endDate = "2015-02-24", the result I am getting is 5 months, 2 weeks, 0 days. Whereas, the result should be 0 months, 0 weeks, 5 days, based on today's date (2015-02-19).

However, if $endDate = "2015-06-16' the result is: 3 months, 3 weeks, 3 days. That is correct.


In reply to Problem with DateTime subtract_datetime by phildeman

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.