On your three outputs, the first one succeeds in getting the date from 30 days ago, which may or may not be one month ago. The second fails because you subtract 28 from the day-of-month, which will have no effect on the rest of the date fields, and then you print out seconds, minutes, and hours anyway. The third succeeds in subtracting 28 days, which will give you "one month ago" if you happen to be in the first 28 days of March in a non-leap year.

"One month ago" and "30 days ago" (or 28 days ago) are not the same thing. If you really want 30 days ago, that's easy: use your first example where you subtract 30*24*60*60 seconds from the current time and get the date from that. But if you really want one month ago, that won't work except when you're crossing a 30-day boundary. And you can't just get the month and decrement it (wrapping around the end of the year if necessary), because what if today is July 31? You'll get June 31, a non-existent date. And then there are leap-years... So if you really want one month ago, use Date::Calc and the Add_Delta_YM function and let the module handle that stuff.

Aaron B.
Available for small or large Perl jobs; see my home node.


In reply to Re: Find 30 days from today's date by aaron_baugher
in thread Find 30 days from today's date by Anonymous Monk

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.