Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Date Operations

by kidd (Curate)
on Jul 02, 2002 at 01:35 UTC ( #178746=perlquestion: print w/replies, xml ) Need Help??
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...


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).


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));


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://178746]
Approved by lemming
and the leaves swirl about...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2018-06-19 08:55 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.