I wrote this subroutine to allow me to generate a MySQL style date for X number of days ago. The ideas is that we want to generate reports for, say, the last week. This lets me pass $days as an argument and get a formatted date (yyyy-mm-dd) returned so that I can use it in the MySQL query as a start date.
use Time::Local; use Time::localtime; ##################################### # number of days ago to begin report ##################################### $days = 7; # calculate the starting date $start_date = &calcDate($days); # calculate the ending date # we use 0 which means current day # or 0 days ago $end_date = &calcDate(0); ########################################################### # subroutine: calcDate # this subroutine takes an integer as an argument # which represents the number of days in the past # that we want to start getting data from ########################################################### sub calcDate { # assign the argument to variable my($offset) = @_; # calculate the number of epoch seconds # in the requested days my $epoch_offset = $offset * 86400; # get the current time & date my $date = localtime; # place parts into variables my $mday = $date->mday; my $mon = $date->mon; my $year = $date->year; # format dates $mon++; $year = $year + 1900; # get the seconds since epoch my $epoch = timelocal($seconds, $minutes, $hours, $mday, $mon, $yea +r); # calculate the number of epoch seconds since requested days ago my $past_epoch = $epoch - $epoch_offset; # get the date of X (requested) days ago my $past_date = localtime($past_epoch); # place parts into variables my $past_mday = $past_date->mday; my $past_mon = $past_date->mon; my $past_year = $past_date->year; # y2k compliance $past_year = $past_year + 1900; #format the date my $date = "$past_year-$past_mon-$past_mday"; # return the formatted date return "$date"; } # end of subroutine

In reply to 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.