My function stops working once I change the timeframe

 
I have a simple function that draws a daily open line for that day and then also for that week but once i change the timeframe of the function for monthly it just stops working 
Heres the daily :
void CreateOpenLine(int barsBack)
  {
// Set up necessary variables
   datetime start_time, end_time;
   double open_price;
   string line_name;
   int i;
   datetime previous_time = 0;

// Loop through the chart's bars
   for(i = 0; i < barsBack ; i++)
     {
      // Get the start time of the current bar
      start_time = iTime(Symbol(), PERIOD_D1, i);
      open_price = iOpen(Symbol(), PERIOD_D1, i);

      // Check if this is the first bar of the day
      if(TimeDay(start_time) != TimeDay(previous_time))
        {
         // set the start_time to the begining of the day
         start_time = start_time - (start_time % 86400);
         end_time = start_time + 86400;
         // Create a unique name for the line
         line_name = "Open Line Daily " + IntegerToString(i)+IntegerToString(ObjectsTotal(0,0, OBJ_TREND) + 1);

         // Draw the line
         if(!ObjectCreate(line_name, OBJ_TREND, 0, start_time, open_price, end_time, open_price))
           {
            // Print an error message if the line could not be created
            Print("Error creating open line: ", GetLastError());
           }
         else
           {
            ObjectSetInteger(0,line_name,OBJPROP_COLOR,OpenLineColorDaily);
            ObjectSetInteger(0,line_name,OBJPROP_WIDTH,OpenLineWeightDaily);
            ObjectSetInteger(0,line_name,OBJPROP_STYLE,OpenLineDailyStyle);
            ObjectSetInteger(0,line_name,OBJPROP_SELECTABLE,true);
            ObjectSetInteger(0,line_name,OBJPROP_RAY,false);
           }
        }

      // Store the current bar's time for the next iteration
      previous_time = start_time;
     }
  }

The weekly 

void CreateOpenLineWeekly(int barsBack)
  {
// Set up necessary variables
   datetime start_time, end_time;
   double open_price;
   string line_name;
   int i;
   datetime previous_time = 0;

// Loop through the chart's bars
   for(i = 0; i < barsBack ; i++)
     {
      // Get the start time of the current bar
      start_time = iTime(Symbol(), PERIOD_W1, i);
      open_price = iOpen(Symbol(), PERIOD_W1, i);

      // Check if this is the first bar of the day
      if(TimeDay(start_time) != TimeDay(previous_time))
        {
         // set the start_time to the begining of the day
         start_time = start_time;
         end_time = start_time + 604800;
         // Create a unique name for the line
         line_name = "Open Line Weekly" + IntegerToString(i)+IntegerToString(ObjectsTotal(0,0, OBJ_TREND) + 1);

         // Draw the line
         if(!ObjectCreate(line_name, OBJ_TREND, 0, start_time, open_price, end_time, open_price))
           {
            // Print an error message if the line could not be created
            Print("Error creating open line: ", GetLastError());
           }
         else
           {
            ObjectSetInteger(0,line_name,OBJPROP_COLOR,OpenLineColorWeekly);
            ObjectSetInteger(0,line_name,OBJPROP_WIDTH,OpenLineWeightWeekly);
            ObjectSetInteger(0,line_name,OBJPROP_STYLE,OpenLineWeeklyStyle);
            ObjectSetInteger(0,line_name,OBJPROP_SELECTABLE,true);
            ObjectSetInteger(0,line_name,OBJPROP_RAY,false);
           }
        }

      // Store the current bar's time for the next iteration
      previous_time = start_time;
     }
  }

Here is the monthly where it just stops working : 

void CreateOpenLineMonthly(int barsBack)
  {
// Set up necessary variables
   datetime start_time, end_time;
   double open_price;
   string line_name;
   int i;
   datetime previous_time = 0;
   end_time = 0;

// Loop through the chart's bars
   for(i = 0; i < barsBack ; i++)
     {
      // Get the start time of the current bar
      start_time = iTime(Symbol(), PERIOD_MN1, i);
      open_price = iOpen(Symbol(), PERIOD_MN1, i);

      // Check if this is the first bar of the day
      if(TimeDay(start_time) != TimeDay(previous_time))
        {
         // set the start_time to the begining of the day
         start_time = start_time;

         if(Month() == 1)
           {
            end_time = start_time + 2678400;
           }
         else
            if(Month() == 2)
              {
               end_time = start_time + 2419200;
              }
            else
               if(Month() == 3)
                 {
                  end_time = start_time + 2678400;
                 }
               else
                  if(Month() == 4)
                    {
                     end_time = start_time + 2592000;
                    }
                  else
                     if(Month() == 5)
                       {
                        end_time = start_time + 2678400;
                       }
                     else
                        if(Month() == 6)
                          {
                           end_time = start_time + 2592000;
                          }
                        else
                           if(Month() == 7)
                             {
                              end_time = start_time + 2678400;
                             }
                           else
                              if(Month() == 8)
                                {
                                 end_time = start_time + 2592000;
                                }


         // Create a unique name for the line
         line_name = "Open Line Monthly " + IntegerToString(i)+IntegerToString(ObjectsTotal(0,0, OBJ_TREND) + 1);

         // Draw the line
         if(!ObjectCreate(line_name, OBJ_TREND, 0, start_time, open_price, end_time, open_price))
           {
            // Print an error message if the line could not be created
            Print("Error creating open line: ", GetLastError());
           }
         else
           {
            ObjectSetInteger(0,line_name,OBJPROP_COLOR,OpenLineColorMonthly);
            ObjectSetInteger(0,line_name,OBJPROP_WIDTH,OpenLineWeightMonthly);
            ObjectSetInteger(0,line_name,OBJPROP_STYLE,OpenLineMonthlyStyle);
            ObjectSetInteger(0,line_name,OBJPROP_SELECTABLE,true);
            ObjectSetInteger(0,line_name,OBJPROP_RAY,false);
           }
        }

      // Store the current bar's time for the next iteration
      previous_time = start_time;
     }
  }


I call the function like this : 

  if(OpenLineDaily)
     {
      datetime D1 =0 ;
      if(D1!=iTime(Symbol(), PERIOD_D1, 0))
        {
         CreateOpenLine(3);
         D1=iTime(Symbol(), PERIOD_D1, 0);
        }
     }
   if(OpenLineWeekly)
     {
      datetime D2 =0;
      if(D2!=iTime(Symbol(), PERIOD_W1, 0))
        {
         CreateOpenLineWeekly(1);
         D2=iTime(Symbol(), PERIOD_W1, 0);
        }
     }

   if(OpenLineMonthly)
     {
      datetime D3 =0;
      if(D3!=iTime(Symbol(), PERIOD_MN1, 0))
        {
         CreateOpenLineMonthly(1);
         D3=iTime(Symbol(), PERIOD_MN1, 0);
        }
     }