in reply to Find 30 days from today's date
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.
|
---|