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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Get the date (MySQL style) for X days ago
by httptech (Chaplain) on Apr 14, 2000 at 21:16 UTC | |
|
RE: Get the date (MySQL style) for X days ago
by btrott (Parson) on Apr 14, 2000 at 21:30 UTC | |
by ikegami (Patriarch) on May 21, 2008 at 20:52 UTC | |
|
RE: Get the date (MySQL style) for X days ago
by Anonymous Monk on Feb 17, 2000 at 10:25 UTC | |
|
Re: Get the date (MySQL style) for X days ago
by Anonymous Monk on May 21, 2008 at 20:34 UTC | |
by repeacock (Novice) on Oct 05, 2009 at 16:08 UTC | |
|
Re: Get the date (MySQL style) for X days ago
by ikegami (Patriarch) on May 21, 2008 at 20:43 UTC |