Or, if you'd rather use POSIX:
my $offset = 65; my $date = strftime "%Y-%m-%d", localtime(time - $offset * 86400);
And if you'd rather have the format for a datetime field:
my $datetime = strftime "%Y-%m-%d %H:%M:%S", localtime(time - $offset * 86400);
In fact, MySQL itself has some date/time functions that do a lot of the formatting for you, so you could use those instead. The FROM_UNIXTIME function takes a Unix timestamp (epoch seconds) and turns it into a MySQL datetime format for you:
my $offset = 65; my $time = $offset * 86400; my $sth = $dbh->prepare_cached(<<SQL); select host from log where stamp > from_unixtime(?) SQL $sth->execute($time);
where "stamp" is a datetime field.

Of course, if what the original poster really wants is a report of records from the last 7 days, all he/she'd have to use is something like this, which lets MySQL do all the work:

my $sth = $dbh->prepare_cached(<<SQL); select host from log where to_days(now()) - to_days(stamp) > ? SQL $sth->execute(7);
where stamp is, again, a datetime field.

And finally, one more way of doing this--this will give you slightly different results than the last query, because the last query is giving you basically anything from the last 7 days; the following will give you anything *within* the last 7 days (a subtle difference):

my $sth = $dbh->prepare_cached(<<SQL); select host from log where stamp > date_sub(now(), interval ? day) SQL $sth->execute(7);

In reply to RE: Get the date (MySQL style) for X days ago by btrott
in thread Get the date (MySQL style) for X days ago by mbreyno

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.