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

I am creating a site that sells products out of my wifes catalogs. I am trying to make it where she can add a "sale" item. or place any item on sale.

I created the admin form to have it where she can login, and change the descriptions, prices, names, and all that. I also added a popup_menu using CGI.pm, that is a "onsale" variable, with the options being 0 and 1. 0 being off and default. I have another popup_menu called saleend, which gives 1 to 6 days, 1 week through 3 weeks and always. So it looks like this:
popup_menu(-name=>"saleend", -values=> ["","1","2","3","4","5","6","7","14","21","always"], -labels=>{ "" => "Select One", "1" => "1 Day", "2" => "2 Days", "3" => + "3 Days", "4" => "4 Days", "5" => "5 Days", "6" => "6 Days", "7" => +"1 Week", "14" => "2 Weeks", "21" => "3 Weeks", "always" => "Everyday +" }, -default=>"", -class=>"select_menu");
My question is this...
Can I do this:
my $saleend = localtime() + ($in{saleend} * 24 * 60 * 60) unless $in{s +aleend} eq "always"; my $saleend = localtime() + (365 * 24 * 60 * 60) if $in{saleend} eq "a +lways";
What I'm trying to do, is ADD that much time, in seconds, to the current date. Then I'm inserting that into the database. That way I can have it automatically turn the sales OFF, after they reach the amount of days she gives them.

I have also tried it like this:
my $saleend = time + ($in{saleend} * 24 * 60 * 60) unless $in{saleend} + eq "always"; my $saleend = time + (365 * 24 * 60 * 60) if $in{saleend} eq "always";
Is there a easier way to do this?

thx,
Richard

Replies are listed 'Best First'.
Re: add to localtime?
by Limbic~Region (Chancellor) on Apr 26, 2003 at 19:58 UTC
    powerhouse,
    Yes, you can do this sort of thing. There are a lot of modules on CPAN for date/time manipulation and my personal favorite is Time::Local. It will allow you to translate a human readable date back into seconds since epoch. It doesn't get much simpler than that unless you want to create a sub to handle calculating the seconds that can be hidden away and be re-used.

    Cheers - L~R

    Update follows:

    my $saleend = get_time($in{'saleend'); my $salestart = get_time($in{'salestart'); # nasty sub hidden away sub get_time { my $input = shift; if ($input eq "always") { return time() + (365 * 24 * 60 * 60); } else { return time() + ($input * 24 * 60 * 60); }

    Cheers - L~R
    Update: Modified localtime to time

      For some reason this code: localtime()+(param("saleend") * 24 * 60 * 60) is putting the output of 604800 which is the param("saleend" * 24 * 60 * 60) (saleend is equal to 7).

      Why would localtime() not have a value? Is it NOT a built in Perl function?

      thx,
      Richard
        localtime() returns either an array of numbers representing seconds, minutes, hours, etc., or a string that represents date/time in a human readable form.
        What you need there is time(), not localtime().

        --perlplexer
Re: add to localtime?
by Mr. Muskrat (Canon) on Apr 26, 2003 at 19:56 UTC

    If I were doing it, I'd probably use Date::Calc. But if all you want is to simplify what you are using, then perhaps:
    my $saleend = localtime() + ($in{saleend} eq "always" ? 31536000 : ($in{saleend}*8640));