You're already using
Date::Calc, so I'd go with Add_Delta_Days:
#!/usr/bin/perl -w
use strict;
use warnings;
use Date::Calc qw{ :all };
my ($year, $mon, $day) = Today();
show_date("Today's date", $year, $mon, $day);
($year, $mon, $day) = Add_Delta_Days($year, $mon, $day, -30);
show_date("30 days ago", $year, $mon, $day);
sub show_date {
my ($label, $year, $mon, $day) = @_;
my $date = sprintf "%04d-%02d-%02d", $year, $mon, $day;
print "$label: $date\n";
}
__END__
[Output]
Today's date: 2013-06-07
30 days ago: 2013-05-08
Update: Oh, now I see why you're getting different answers. It's because in:
$date[3] -= 4 * 7;
...
my $month_ago_3 = strftime('%Y-%m-%d', localtime(time - 4 * 7 * 86400
+));
You're subtracting 4 * 7 days (ie. 4 weeks), rather than 30 days.
Does that help?
say
substr+lc crypt(qw $i3 SI$),4,5
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.