Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    best way to work out iso date range

    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
    Code:
      #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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #2
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    progress made slightly

    Code:
      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

    Code:
      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
    Last edited by bazz; 11-18-2009 at 01:55 AM.
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •