Kevin Old wrote:
> 
> Hello everyone,

Hello,

> I'm trying to create an array of months that will be a continuous
> "Moving View of Months".  I will have an page served to users that has a
> table of the past 12 months.  For instance, if I were to pull up the
> page today, it would display Apr 02 - Mar 03.  Next month it will be May
> 02 - Apr 03.
> 
> I'm able to create the array, but think there might be a much more
> efficient way to do it.
> 
> Basically what I do is use UnixDate from Date::Manip to get the date
> from 11 months ago (split that into an array for &date_range) then I get
> a list of dates from 11 months ago until today.  I've yet to write the
> code to parse the array of dates and pluck out the first of each month
> and put into an array.
> 
> Have a look at my code below.  Any pointers/suggestions are appreciated!
> 
> #!/usr/bin/perl -w
> #
> use strict;
> use Date::Calc qw(Delta_DHMS Delta_Days Add_Delta_Days Date_to_Text
> Today);
> use Date::Manip qw(UnixDate);
> 
> my $ea = &UnixDate("-11 months", "%Y,%m,%d");
> print "$ea\n";
> my @elevenago = split/,/, $ea;
> 
> my @today = Today();
> my @dates = ();
> my @range = &date_range(@elevenago, @today);
> # @range is a list of days from @elevenago to @today
> 
> sub date_range {
> 
>         my(@date) = (@_)[0,1,2];
>         my(@list);
>         my($i);
> 
>         $i = Delta_Days(@_);
>         while ($i-- >= 0)
>         {
>                         push( @list, [ @date ] );
>                         @date = Add_Delta_Days(@date, 1) if ($i >= 0);
>         }
>         return(@list);
> }#end date_range


$ perl -e'
use Time::Local;

use constant DAY         => 3;
use constant MONTH       => 4;
use constant YEAR        => 5;
use constant SEC_PER_DAY => 86_400;
my @month_range = 0 .. 11;
my $today = time;

my @before = localtime $today;
my $month = $before[ MONTH ];
$before[ MONTH ] = $month_range[ $before[ MONTH ] - 11 ];
$before[ YEAR ]-- if $before[ MONTH ] > $month;

print scalar( localtime timelocal @before ), "\n";
print scalar( localtime $today ), "\n\n";

my @date_array;
for ( my $secs = timelocal @before; $secs <= $today; $secs += SEC_PER_DAY ) {
    my ( $day, $mon, $year ) =  (localtime $secs)[ 3 .. 5 ];
    push @date_array, [ $year + 1900, $mon + 1, $day ];
    }

for my $date ( @date_array ) {
    print "@$date\n";
    }
'
Sun Apr 21 14:10:33 2002
Fri Mar 21 14:10:33 2003

2002 4 21
2002 4 22
2002 4 23
2002 4 24
2002 4 25
2002 4 26
2002 4 27
2002 4 28
2002 4 29
2002 4 30
2002 5 1
2002 5 2
2002 5 3
...
2003 3 14
2003 3 15
2003 3 16
2003 3 17
2003 3 18
2003 3 19
2003 3 20
2003 3 21




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to