There's more than one way to do things PerlMonks

### Re: How to subtract date by 1 day

by Zaxo (Archbishop)
 on Dec 13, 2007 at 10:22 UTC ( #656794=note: print w/replies, xml ) Need Help??

in reply to How to subtract date by 1 day

Subtract 24*60*60 seconds from the unix time before using localtime to split into units.

I recommend POSIX::strftime for converting that to a string.

```use POSIX 'strftime';
print strftime "%Y_%m_%d_%H_%M_%S/n", localtime() - 24 * 60 * 60;
prints this time yesterday in your format.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: How to subtract date by 1 day
by ikegami (Pope) on Dec 13, 2007 at 15:08 UTC

If by day, the OP means 24 hours, that's fine. If he just wants to move back the date one day without changing the time, your solution is wrong since not every day has 24*60*60 seconds. Use timegm+gmtime to do date manips.

```use POSIX       qw( strftime );
use Time::Local qw( timegm );

my (\$d,\$m,\$y) = (localtime())[3,4,5];

print(strftime("%Y_%m_%d\n",
gmtime(timegm(0,0,0,\$d,\$m,\$y) - 24*60*60)
));

With time:

```use POSIX       qw( strftime );
use Time::Local qw( timegm );

my (\$sec,\$min,\$hour,\$d,\$m,\$y) = localtime();
(\$d,\$m,\$y) = (gmtime(timegm(0,0,0,\$d,\$m,\$y) - 24*60*60))[3,4,5];

print(strftime("%Y_%m_%d_%H_%M_%S\n", \$sec,\$min,\$hour,\$d,\$m,\$y));

Create A New User
Node Status?
node history
Node Type: note [id://656794]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2018-03-24 13:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (298 votes). Check out past polls.

Notices?