Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9012897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:07:01+00:00 2026-06-16T03:07:01+00:00

I have been using the following code for the last few months, which loops

  • 0

I have been using the following code for the last few months, which loops through a period of months from a predefined date until it gets to today’s date.

use Date::Pcalc qw(:all);

$startDay = 1;
$startMonth = '4';
$startYear = '2009';

$dateToday = `date +%Y-%m-%d`;
($yt,$mt,$dt) = split(/\-/,$dateToday);

while ($endMonth <= $mt || $startYear < $yt ) {

if ($startMonth eq '12') {
    $endMonth = 1;
    $endYear = $startYear + 1;
  } else {
    $endMonth = $startMonth + 1;
    $endYear = $startYear;
  }

  if ($startMonth eq '12') {
    $endYear = $startYear + 1;
  }

  ($meYear,$meMonth,$meDay) = Add_Delta_Days($endYear,$endMonth,$startDay,-1);
  $endOfMonth = "$meYear-$meMonth-$meDay";
  $monthText = Month_to_Text($startMonth);

  $startDate = "$startYear-$startMonth-1";
  $endDate = "$endYear-$endMonth-1";

print "$startDate - $endDate\n";

if ($startMonth eq '12') {
    $startMonth = 1;
    $startYear++;
  } else {
    $startMonth++
  }

}

This has been working great for the last few months, but I’ve realised that now in December, as $endmonth will never be greater $mt (12), this causes an infinite loop.

I’ve not been able to figure out any alternate way of doing this. I feel like I should be able to fix this relatively easily but I seem to be having severe ‘developer’s block’

Thanks in advance to anyone who can assist.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:07:02+00:00Added an answer on June 16, 2026 at 3:07 am

    I think you have a couple of problems with your code. But lets get to the first problem which is the enddate in month 12 which causes a loop in this statement:

        while ($endMonth <= $mt || $startYear < $yt ) {
    

    OK what you should do is something like this, once you have the current date, year month and day. You will notice that others have suggested different way to get the current date You should take up this suggestion. However once you have the date this code below should be adopted:

        ($yt,$mt,$dt) = split(/\-/,$dateToday);
        # the line below will create a date like 201212 (yyyy mm) but if the month is a 1 digit month it will place a 0 in front of it to ensure your yymm variable always holds 6 characters in the format of yyyy mm - ok
        my $yymm = $yt . ${\(length($mt) == 1 ? '0' : '')} . $mt;
        # Now lets check the end date against the yymm
        # initialise end date as end_yymm - again it inserts a 0 for single digit month
        my $end_yymm = $startyear . ${\(length($startMonth) == 1 ? '0' : '')} . $startMonth;
        # the above should get the date as '200904' from your code provide
        # the while will check end_yymm like 200904 < 201212 - yes it is...
        ## the end_yymm will keep getting incremented each month and so will the year component at the end of each year until it reaches 201212
        ## then the question 201212 < 201212 will cause the while to end
        ## If you want it go into 201301 then say while ($end_yymm <= $yymm) {
        ## Hope you get the picture
        while ($end_yymm < $yymm) {
    
        if ($startMonth eq '12') {
            $endMonth = 1;
            $endYear = $startYear + 1;
        } else {
          $endMonth = $startMonth + 1;
          $endYear = $startYear;
        }
    
        ## Now this one seems to be repeating the endYear calculation as above - to me it seems redundant - maybe get rid of it
        if ($startMonth eq '12') {
           $endYear = $startYear + 1;
        }        
    
        ## Now that you have the end year and month incremented setup the end_yymm variable again to be picked up in the while statement:
        $end_yymm = $startyear . ${\(length($startMonth) == 1 ? '0' : '')} . $startMonth;
    
         # ...... carry on with the rest of your code
    
        } # end the while loop
    

    And that should do it.

    All the best

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following code for the Share Price Application I have been
Using the following code I have been able to display a text message when
I have been using the following sort: var query = _cityRepository.GetAll( .OrderBy(item => item.RowKey.Substring(0,
I have been using the following CSS to apply an effect at the bottom
I have been using the following to add a dynamic link on a page
I have been following the following post on using multiple ItemTemplates in a ListView
I have been doing some research for using MSMQ. Following 2 gave me fundamental
Following the standard instruction for using AES algorithm, I have not been able to
I have been developing a project in python for the last six months, and
I've been using Think Python to learn programming over the last few days. Today

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.