And if you'd rather have the format for a datetime field:my $offset = 65; my $date = strftime "%Y-%m-%d", 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 $datetime = strftime "%Y-%m-%d %H:%M:%S", localtime(time - $offset * 86400);
where "stamp" is a datetime field.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);
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:
where stamp is, again, a datetime field.my $sth = $dbh->prepare_cached(<<SQL); select host from log where to_days(now()) - to_days(stamp) > ? SQL $sth->execute(7);
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |