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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |