...

View Full Version : best way to work out iso date range



bazz
11-18-2009, 12:46 AM
Hi,

I have a grid on my web page sort of like a calendar. left col is the room number and the next 31 or so cols to the right of it, are the dates I want to show. eg a whole month of up to 31 (as appropriate) or 31 days from say 15. could equally be 31 days from 28.

the values being passed to this scirpt are $year, $month, $date.

The following code gets me the number of days in a given month and then I loop through those as shown


#Days_in_Month
my $days = Days_in_Month($year,$month); # number of days this month

foreach my $day_number (1..$days)
{


# make single-digit numbers double-digit.
$day_number = sprintf("%02d", $day_number);
$month = sprintf("%02d", $month);

my $full_iso_date = "$year-$month-$day_number";

}



This obviously won't work for the scenario where 15 days are in one month and 15 days are in the next month. so would you recommend:

1. In place of 'Days_in_Month' I should convert to Day_of_Year and so they would be a consecutive range from say 15 thru 46

2. in the loop, I then convert back to the date of the month so I shall OUTPUT 15-31 followed by 1-16?

OR would you know a better way?

bazz

bazz
11-18-2009, 01:51 AM
progress made slightly :)



my $first_year = $cgi->param('start_year');
my $first_doy = $cgi->param('doy');
$first_year = '2009'; # temp hard code value
$first_doy = '289'; # temp hard code value

my $Dd = '31'; #number_of_days_to_display

# build the start_iso
my ($first_year,$first_month,$first_date) = Add_Delta_Days($first_year,1,1, $first_doy - 1);

#build_last_iso
my ($last_year,$last_month,$last_date) =
Add_Delta_Days($first_year,$first_month,$first_date,
$Dd);

print qq( $first_year,$first_month,$first_date<br />
$last_year,$last_month,$last_date
);



But I get an issue when one year becomes the next.

$first_doy = '289';
my $Dd = '31'; #number of days to show
my $last_doy = ($first_doy + $Dd);

my 'foreach' loop will therefore be




foreach my $day_number ( $first_doy .. $last_doy) {

# make single-digit numbers double-digit.
$day_number = sprintf("%02d", $day_number);
$first_month = sprintf("%02d", $first_month);

my $full_iso_date = "$year-$month-$day_number";





How can I make this work across two years eg 360 thru 25? Do I need to get the number of days in $first_year and then add/subtract and play about using a conditional, to mkae days 366 - 391 readjust to 01 - 26?

bazz



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum