Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Adding to dates

by Anonymous Monk
on Aug 08, 2002 at 13:08 UTC ( [id://188579]=perlquestion: print w/replies, xml ) Need Help??

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/"

      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__

        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,

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


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/


    ppm install Date::Tie


    perl -MCPAN -e shell install Date::Tie
      thansk, when i try to run it i get the error: Can't locate Date/ 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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://188579]
Approved by RMGir
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-07-25 20:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.