http://www.perlmonks.org?node_id=178746

kidd has asked for the wisdom of the Perl Monks concerning the following question:

I want to create a CGI so I can send reminders in a certain time, but Im having an awful time adding days to my date(huh?)

Maybe I should give an example...

Lets say today is 20/10/2002 dd/mm/yyyy

and the user wants to be reminded in 15 days, so the remind day must be: 4/11/2002 considering that October has 31 days(correct me if Im wrong)...

But Im done with the if/else 'cause it makes the process slower and has a lot of bugs...

If someone could give me and idea Ill be grateful, since im desperate...

Thanks

Replies are listed 'Best First'.
Re: Date Operations
by emilford (Friar) on Jul 02, 2002 at 01:44 UTC
    Consider using the Date::Calc module; it's incredible for such tasks.

    If you're looking to add "$offset" days to your current date, you could use something like this:
    use Date::Calc qw(Add_Delta_Days); ($year, $month, $day) = Add_Delta_Days($old_year, $old_month, $old_day +, $offset);
    This will return the new year, month, and day after adding the offset ($offset). Once you have those values, I think you can take it from there. Look up the documentation for this module. There are so many more features available and it will save you a ton of time and headaches!

    Hope this helps, Eric
Re: Date Operations
by mojotoad (Monsignor) on Jul 02, 2002 at 03:26 UTC
    I was going to suggest that you need not bother with any of the date modules for such a "simple" calculation -- you could merely add $days*60*60*24 to the seconds since epoch followed by a reverse transformation.

    However, in 2002, DST reverts on October 27th, so such a calculation would fail by coming up one hour short.

    It is precisely due to such gotchas that the date modules come in handy. I've had good results with Date::Calc (fast but requires a C compiler), Date::Manip (slower, possibly more complete for esoteric dates, no C), and Time::Piece (C compiler).

    Matt

Re: Date Operations
by joshua (Pilgrim) on Jul 02, 2002 at 03:33 UTC
    If you just need the date and not the time, and you're not running the script at 12 am or 11 pm, you could use something like this...
    use strict; use POSIX qw[strftime]; my $date = strftime('%d/%m/%Y', localtime(time + 60 * 60 * 24 * 15));

    Joshua