Yes,  a month from the first to the last day.  This is how I'm doing it right
now which seems to be a lot of code which should be able to be pruned:
#Declare the variables we'll need
my $count = 1;
my $febDays;
my @days;
my %time;
my %months31 = (
        "01" => undef,
        "03" => undef,
        "05" => undef,
        "07" => undef,
        "08" => undef,
        "10" => undef,
        "12" => undef,
);
my %months30 = (
        "04" => undef,
        "06" => undef,
        "09" => undef,
        "11" => undef,
);
my %months = (
        "01" => "Jan",
        "02" => "Feb",
        "03" => "Mar",
        "04" => "Apr",
        "05" => "May",
        "06" => "Jun",
        "07" => "Jul",
        "08" => "Aug",
        "09" => "Sept",
        "10" => "Oct",
        "11" => "Nov",
        "12" => "Dec",
);

my @date; #  = (localtime)[4,5];
my $month = (sprintf '%02d', (localtime)[4]); #$date[0]);
my $year      = (localtime)[5] + 1900; #$date[1] + 1900;

# If the value of the 'month' variable is '00' we are in January so need
# to set the $month to 12 in order to get December's data.  Any other
# value is spot on.  The reason being that  while (localtime) will produce
# a value based  on a startpoint of '00', we are shifting this back one by
# making '00' equal to '12' causing all other numbers to match up to their
# respective month.
if ($month == '00') {
        $month = 12;
        $year--;
}

# We need to determine if the current year is a leap year so we use the
# right number of days for Feb.
if (isleap($year)) {
        $febDays = 29;
}else{
        $febDays = 28;
}

 Determine if the month requested has 31, 30 or 28 days and build
# our days array to match
if (exists($months31{$month})){
        while ($count <= 31) {
                push @days, (sprintf '%02d', $count);
                $count++;
        }
}elsif (exists($months30{$month})){
        while ($count <= 30) {
                push @days, (sprintf '%02d', $count);
                $count++;
        }
}else{
        while ($count <= $febDays) {
                push @days, (sprintf '%02d', $count);
                $count++;
        }
}

That's just a keister-load of code just to create an array of 28/29, 30 or 31 
dates.

Mathew
Keep up with me and what I'm up to: http://theillien.blogspot.com


Mathew Snyder wrote:
> A while ago I had posted requesting help with a long block of code that would 
> do
> all kinds of stuff dealing with the date.  It turned out to not work despite
> being technically, correct.  Instead of getting help with it, Mr. Phoenix
> provided me with a block of code that did what I needed but much more 
> concisely.
>  And, more importantly, correctly.
> 
> for (1 .. 7) {
>   $time -= 24*60*60;
>   my @date = (localtime($time))[3 .. 5];
>   push @days, (sprintf '%02d', $date[0]);
>   push @months,(sprintf '%02d',$date[1] + 1);
>   push @years, $date[2] + 1900;
>   push @searchDate, join "-", ($date[2] + 1900), (sprintf '%02d',$date[1] + 
> 1),
> (sprintf '%02d', $date[0]);
> }
> 
> This will give me a weeks worth of dates regardless of whether or not the 
> month
> flips over in the middle of the week.
> 
> What I'd like to do now is modify this or figure out a similar block of code
> that will do the same only for an entire month.  The thing I see being a 
> problem
> though is that some months have 30 days, some months have 31 days and February
> has 28/29 days.  This makes it pretty much impossible to just do a for loop
> utilizing (1..whatever).  How can I populate an array of an entire month's 
> worth
> of dates without worrying about how many days the month has?
> 
> Mathew

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to