Your code has a bug in it if you execute it at certain times of the year. You assume that every day has 86400 seconds, but not all of them do.
Change
to# 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);
# get the seconds since epoch my $epoch = timegm(0,0,0, $mday, $mon, $year); # 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 = gmtime($past_epoch);
Note that even though timegm and gmtime is used, the result is using the local time zone as you intended.
See Re^2: Get the date (MySQL style) for X days ago for an example of when your code fails.
In reply to Re: Get the date (MySQL style) for X days ago
by ikegami
in thread Get the date (MySQL style) for X days ago
by mbreyno
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |