http://qs321.pair.com?node_id=188579

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

Hi Monks, I am having problem writing a script that will take the current date from a linux system put it into a variable i.e $suspenddate and then adding two weeks to the date in that variable. I need this for a website i am creating that creates accounts on my servers and gives users a 2 week trial period. So far i have had no luck at all. Thanks Chris

Replies are listed 'Best First'.
Re: Adding to dates
by simeon2000 (Monk) on Aug 08, 2002 at 13:15 UTC
    This is really easy to do. Here:

    # define the seconds in a week use constant WEEK => 60 * 60 * 24 * 7; my $now = time(); my $then = $now + WEEK * 2;

    Or, to one-line it:

    my $then = time() + 1209600;

    --
    perl -e "print qq/just another perl hacker who doesn't grok japh\n/"
    simeon2000|http://holdren.net/

      just tried your solution simeon and the ourtput from $then was 1030012664? I need the output to be in the form of YY-MM-DD Thanks

        So, just to expand a little on simeon2000's answer:

        #! /usr/bin/perl use strict ; use warnings ; $|++ ; use constant WEEK => 60 * 60 * 24 * 7 ; my @later = localtime( time + ( WEEK * 2 ) ) ; my $fmt_date = sprintf "%04d-%02d-%02d", $later[5] + 1900, $later[4], $later[3] ; print $fmt_date, "\n" ; __END__

        _______________
        DamnDirtyApe
        Those who know that they are profound strive for clarity. Those who
        would like to seem profound to the crowd strive for obscurity.
                    --Friedrich Nietzsche
      sorry peeps, i wasn't descriptive enough. This is what i realy need to do: get the current date, add 14 days to it, format it to YYYY-MM-DD and then put it into a variable so that i can use it on a command like... adduser myself -e $expirydate
Re: Adding to dates
by aufrank (Pilgrim) on Aug 08, 2002 at 13:47 UTC

    good morning!

    simeon2000 offered a great solution, but I thought I'd point you in the direction of two excellent date-manipulation modules: Date::Calc (the link is to a gzipped archive which has a lot of useful calendar modules in it), and Date::Manip. I've used Date::Calc a number of times and find it a pleasure to work with. I've heard equally good things about Date::Manip.

    If your date manipulation needs get any more complicated, or you just want to learn the ins and outs of a few useful modules, I'd highly suggest checking out the pods.

    good luck with your project,
    --au

Re: Adding to dates
by derby (Abbot) on Aug 08, 2002 at 14:45 UTC
    #!/usr/bin/perl use Date::Manip; $two_weeks = UnixDate( DateCalc( "today", "+14 days" ), "%y-%m-%d" ); print $two_weeks, "\n";

    -derby

Re: Adding to dates
by fglock (Vicar) on Aug 08, 2002 at 14:48 UTC

    Hi Chris.

    There are many ways to do it. This is another one:

    use Date::Tie; tie my %date, 'Date::Tie'; $date{week} += 2; # today + 2weeks print "$date{month}/$date{day}\n";

    update: Re: module installation -- error: Can't locate Date/Tie.pm

    Windows:

    ppm install Date::Tie

    Unix:

    perl -MCPAN -e shell install Date::Tie
      thansk, when i try to run it i get the error: Can't locate Date/Tie.pm in @inc any ideas?
        i take that last question back. is it possible to do this without requiring any additional modules? James
Re: Adding to dates
by ambrus (Abbot) on Mar 09, 2004 at 17:09 UTC