Simple version
Since
time is the current moment in time,
as a Unix Epoch second value, just subtract 86400
from that, and use
localtime in a scalar context to make a date string:
my $yesterday = localtime (time - 86400);
You can extract month/day/year from that string,
or use the more general list context version and
do the processing yourself:
my @yesterday_values = localtime(time - 86400);
More precise version
Those versions suffer from the "daylight savings time" switch, and so it fails once or twice a year when a day has 25 hours or 23 hours in it. To get around that,
aim for something "mid-day" yesterday, since you don't care about the specific time:
my $current_hour = (localtime)[2];
my $yesterday_around_noon = localtime(time - ($current_hour + 12) * 36
+00);
This works by getting us back to roughly midnight today, then going back another 12 hours. Even if the day is 23 or 25 hours, this'll still be within the mid-day span.
-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.
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.