Forum Moderators: coopster & phranque

Message Too Old, No Replies

Subtracting 30 days from yesterday's date

         

kelly_boyce

12:57 pm on Jul 31, 2003 (gmt 0)

10+ Year Member



Hello Everyone, I have been looking all over for a perl code that subtracts 30 days. I need a cgi code that will subtract 30 days from yesterday's date (not todays') and then display it in dd-mm-yyyy format. also, dd, mm, yyyy should be variables so that i can print them seperatly if needed.

Thanx in advance

Robber

3:25 pm on Jul 31, 2003 (gmt 0)

10+ Year Member



Hi Kelly,

Welcome to WW.

One way to do it would be to use the time() function to get the number of seconds since 1970 (this is epoch time - used by some scripting languages).

You could then work out the number of seconds in a day, multiply by 31 (yesterday is -1, -30 = 31), then I think you could use localtime() to format the result.

But you'll have to check the documentation for the specifics unless someone else round here knows it off the top of ther head.

Hope that helps.

Storyteller

8:01 am on Aug 2, 2003 (gmt 0)

10+ Year Member



Check out Time::Piece from CPAN. It does date arithmetic easily.

ShawnR

10:09 pm on Aug 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See also [webmasterworld.com...]

dkubb

10:44 pm on Aug 2, 2003 (gmt 0)

10+ Year Member



One of the best Date modules on CPAN I've recently begun using is called DateTime. The interface is clean and easy to use, despite being extremely powerful. It handles "date math" better than most other modules, taking into account things like leap seconds/years properly, as well as factoring in daylight savings time into the calculations.

What you want can be done as simply as:

use DateTime;

my $dt = DateTime->now->subtract(days => 31);

print $dt->dmy, "\n"; #the date in dd-mm-yyyy format

my $year = $dt->year;

my $month = $dt->month;

my $day = $dt->day;