Led fade not working correctly

I got this code from the internet and implemented it with the Blynk app. I added a second argument to the SetLed() function being int fadeout as I want to set different fadein and fadeout times.
The programme it the fadein (ie. sunrise) the middle part the holding works ok its the last part, the fade out which does not work. What tends to happen is the led switches off totally just before commencement of fadeout.
I would appreciate if somebody could have a look and tell me where I am going wrong. I am using an ESP 8266 so therefore my byte are not 0-255 but 0-1023 and I am using millis instead of minutes in order to get a smoother fade.

BLYNK_WRITE(V2)  //CH1 TIME IMPUT WIDGET
{
  TimeInputParam t(param);
  SThour = t.getStartHour();
  STmin = t.getStartMinute();
  STsec = t.getStartSecond();
  SPhour = t.getStopHour();
  SPmin = t.getStopMinute();
  SPsec = t.getStopSecond();
  dayNumber = weekday();
  activeToday = false;
  unsigned int OnDays1 = 0;
  for (int i = 1; i <= 7; i++)    //Process weekdays (1-Mon, 2-Tues, 3-Wed, 4-Thurs, 5-Fri, 6-Sat, 7-Sun)
  {
    if (t.isWeekdaySelected(i))
    {
      OnDays1++;
      if(i==dayNumber)
      {
        activeToday = true;
      }
      if(t.hasStartTime())
      {
        CH1Start = (((SThour*3600) + (STmin*60) + STsec)*1000);   //Time in millis
        Blynk.virtualWrite(V8, CH1Start);
      }
      if(t.hasStopTime())
      {
        CH1End = (((SPhour*3600) + (SPmin*60) + SPsec)*1000);    //Time millis
      }
    }
    if (OnDays1 == 0)
    {
      activeToday = false;
    }
  }
}

BLYNK_WRITE(V3)   //CH1 FADEIN TIME WIDGET
{
  FadeINmin = param.asInt();
  FadeINmillis = map(FadeINmin, 0, 120, 1, 7200000);   //Fadein param is in millis as set by widget
  Blynk.virtualWrite(V6, FadeINmillis);
}

BLYNK_WRITE(V4)   //CH1 FADEOUT TIME WIDGET
{
  FadeOUTmin = param.asInt();
  FadeOUTmillis = map(FadeOUTmin, 0, 120, 1, 7200000);      //Fadeout param is in minutes as set by widget
  Blynk.virtualWrite(V7, FadeOUTmillis);
}

BLYNK_WRITE(V5)   //CH1 UV INTENSITY WIDGET
{
  int PWMinput = param.asInt();
  maxPWM = map (PWMinput, 0, 100, 0, 1023);   //Intensity param set with widget
  Blynk.virtualWrite(V9, maxPWM);
}

void SetLed(int timenowMillis, 
            int ledPin, 
            int start, 
            int fadein, 
            int period, 
            int fadeout, 
            int setPWM)
{
  if (timenowMillis > start && timenowMillis <= start + fadein)
  {
    analogWrite(ledPin, map(timenowMillis - start, 0, fadein, 0, setPWM)); //Fade in state
  }
  if (timenowMillis > start + fadein && timenowMillis <= start + period - fadeout)       
  {
    analogWrite(ledPin, setPWM);                                      //Holding state
  }
  if (timenowMillis > start + period - fadeout && timenowMillis <= start + period)
  {
    analogWrite(ledPin, map(timenowMillis - start + period - fadeout, 0, fadeout, setPWM, 0)); //Fade out state
  }
}

void SyncCh()
{
  sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
  TotalMillis = (((hour()*3600) + (minute()*60) + second()) * 1000);
  CH1Period = CH1End - CH1Start;
  Blynk.virtualWrite(V10, CH1Period);
  Blynk.virtualWrite(V11, TotalMillis /1000);
  if (TotalMillis > CH1Start && TotalMillis < CH1Start + CH1Period)
  {
    SetLed(TotalMillis, CH1, CH1Start, FadeINmillis, CH1Period, FadeOUTmillis, maxPWM);
  }
  else  {   //night
    analogWrite(CH1, 0);
  }
}

What happens is that the variables you chose to store and calculate time with are both too small and signed.
Your millis time calculations are done wrong as well but… you did use blynk instead of rolling your own fades.

Arduino millis() returns an unsigned long with ms since startup. It can be used to time intervals up to 49.71-some days.
Arduino micros() returns an unsigned long with us since startup. It can be used to time intervals up to 71.58-some minutes.

Unsigned int can store the low 16 bits of millis() returns with maximum interval of 65.535 seconds.

By using subtraction of unsigned integers the same elapsed = end - start formula works correctly even across rollover.

if ( millis() - startMillis >= intervalMillis )
{
// this code runs when interval has elapsed
// if it turns a led ON/OFF, it’s blink code — vary the ratio to change brightness
startMillis += intervalMills(); // correct even when the if is > instead of =
}

if ( intervalMillis > 0 ) // this is a 1-shot trigger, runs when set
{
if ( millis() - startMillis >= intervalMillis )
{
// this code runs when interval has elapsed
intervalMillis = 0; // only runs once per trigger set
}
}

if ( intervalMillis > 0 ) // this is a 1-shot trigger, runs when set
{
if ( millis() - startMillis < intervalMillis )
{
// this code runs until interval has elapsed
intervalMillis = 0; // only runs once per trigger set
}
}

All the variables declared are unsigned long

AquArt Blynk CalcReactor

WRITTEN BY MARIANO PROIETTI
16 CRANE AVENUE
ISLEWORTH
MIDDLESEX, TW7 7JL
TEL. 07771 528 969
*/

/*BLYNK APP
 */

//Blynk Authorisation code : " XXXXXXXXXXXXXXXXXXXXXX " //
 
//LIBRARIES//
                 

#include <ESP8266WiFi.h>         
#include <BlynkSimpleEsp8266.h>                 //MUST BE IN THIS ORDER
#include <TimeLib.h>
#include <WidgetRTC.h>
WidgetRTC rtc;
BlynkTimer timer;

//WIFI Variables
char auth[] = "";   //Master auth
char ssid[] = "proietti office";
char pass[] = "";


//PIN ASSIGNMENTS//

//RTC & TIME IMPUT ATTRIBUTES & VARIABLES
char currentTime[9];    //RTC time
char currentDate[11];   //RTC date

int SThour;
int STmin;
int STsec;
int SPhour;
int SPmin;
int SPsec;
int dayNumber;
bool activeToday = true;

#define CH1 D4

unsigned long TotalMillis = 0;
unsigned long CH1Start;
unsigned long CH1End;
unsigned long FadeINmin;
unsigned long FadeINmillis;
unsigned long FadeOUTmin;
unsigned long FadeOUTmillis;
int maxPWM;
unsigned long CH1Period;
byte rtcMins, oldMins, rtcHrs;



//CONNECT TO BLYNK SERVER//
BLYNK_CONNECTED()
{
  Blynk.syncAll();
  rtc.begin();
}



void setup() 
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  //analogWrite(CH1, 0);  
  rtc.begin();
  setSyncInterval(30);
  timer.setInterval(1000L, ClockDisplay);
  timer.setInterval(100L, SyncCh);
  //OTHER SET UP STUFF HERE
}

void loop()
{
Blynk.run();
timer.run();

}

//END//

I am not using millis to fade I am using the RTC. I am trying to set the fade to work with the time so that if there is a powercut at re-boot the leds will be set correctly in their setup according to the time.
The Blynk app is working correctly (please see pic where all the variables are shown (similar to serial.Print but on phone).
Also below is the original sketch where I borrowed the fade code from. You will notice also the when using Blynk you have to keep your loop clean in order to not flood the server. This is achieve by using the BlynkTimer which is based on the SimpleTimer library.

/*
 Arduino LED controller for reef aquariums

// paramterized LED lighting control for reef aquariums
// use a DS1307 RTC to kick off lighting schedules
//   fade up on a given slope
//         delay a set amount
//         fade back down
//   such that the photoperiod lasts the correct amount of time


//  Circuit
// 
// PWM pins described below connected to dimming circuits on drivers
// ledMaster pin below connected to a 120V AC relay to turn the LED drivers on and off (optional)
// grounds from drivers connected to arduino ground
// DS1307 RTC connected via I2C
//
//
//

*/

// Pins to control LEDs. Change these if you're using different pins.
int blueLed = 9;     // LED PWM channel for blues
int whiteLed = 10;   // LED PWM channel for whites

// Set up RTC
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68

// RTC variables
byte second, rtcMins, oldMins, rtcHrs, oldHrs, dayOfWeek, dayOfMonth, month, year;

// Other variables. These control the behavior of lighting. Change these to customize behavoir
int minCounter = 0;         // counter that resets at midnight. Don't change this.
int blueStartMins = 480;    // minute to start blues. Change this to the number of minutes past
                            //    midnight you want the blues to start.
int whiteStartMins = 480;   // minute to start whites. Same as above.
int bluePhotoPeriod = 510;  // photoperiod in minutes, blues. Change this to alter the total
                            //    photoperiod for blues.
int whitePhotoPeriod = 510; // photoperiod in minutes, whites. Same as above.
int fadeDuration = 60;      // duration of the fade on and off for sunrise and sunset. Change
                            //    this to alter how long the fade lasts.
int blueMax = 255;          // max intensity for blues. Change if you want to limit max intensity.
int whiteMax = 255;         // max intensity for whites. Same as above.


/****** LED Functions ******/
/***************************/
//function to set LED brightness according to time of day
//function has three equal phases - ramp up, hold, and ramp down
void setLed(int mins,    // current time in minutes
            int ledPin,  // pin for this channel of LEDs
            int start,   // start time for this channel of LEDs
            int period,  // photoperiod for this channel of LEDs
            int fade,    // fade duration for this channel of LEDs
            int ledMax   // max value for this channel
            )  {
  if (mins > start && mins <= start + fade)  {
    analogWrite(ledPin, map(mins - start, 0, fade, 0, ledMax));
  }
    if (mins > start + period && mins <= start + period - fade)  {
    analogWrite(ledPin, ledMax);
  }
    if (mins > start + period - fade && mins <= start + period)  {
    analogWrite(ledPin, map(mins - start + period - fade, 0, fade, ledMax, 0));
  }
}

/***** RTC Functions *******/
/***************************/
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers.
void setDateDs1307(byte second,        // 0-59
                   byte minute,        // 0-59
                   byte hour,          // 1-23
                   byte dayOfWeek,     // 1-7
                   byte dayOfMonth,    // 1-28/29/30/31
                   byte month,         // 1-12
                   byte year)          // 0-99
{
   Wire.beginTransmission(DS1307_I2C_ADDRESS);
   Wire.send(0);
   Wire.send(decToBcd(second));
   Wire.send(decToBcd(minute));
   Wire.send(decToBcd(hour));
   Wire.send(decToBcd(dayOfWeek));
   Wire.send(decToBcd(dayOfMonth));
   Wire.send(decToBcd(month));
   Wire.send(decToBcd(year));
   Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
          byte *minute,
          byte *hour,
          byte *dayOfWeek,
          byte *dayOfMonth,
          byte *month,
          byte *year)
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  *second     = bcdToDec(Wire.receive() & 0x7f);
  *minute     = bcdToDec(Wire.receive());
  *hour       = bcdToDec(Wire.receive() & 0x3f);
  *dayOfWeek  = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month      = bcdToDec(Wire.receive());
  *year       = bcdToDec(Wire.receive());
}

void setup()  { 
  
// init I2C  
  Wire.begin();
} 

/***** Main Loop ***********/
/***************************/
void loop()  { 
  // get time from RTC and put in hrs and mins variables
  getDateDs1307(&second, &rtcMins, &rtcHrs, &dayOfWeek, &dayOfMonth, &month, &year);
  minCounter = rtcHrs * 60 + rtcMins;

  // determine if it is day or night, and act accordingly
  if ((minCounter > blueStartMins || minCounter > whiteStartMins)
           && (minCounter < blueStartMins + bluePhotoPeriod || minCounter < whiteStartMins + whitePhotoPeriod))  {   //day
    // set LED states
    setLed(minCounter, blueLed, blueStartMins, bluePhotoPeriod, fadeDuration, blueMax);
    setLed(minCounter, whiteLed, whiteStartMins, whitePhotoPeriod, fadeDuration, whiteMax);
  }
  else  {   //night
    analogWrite(blueLed, 0);
    analogWrite(whiteLed, 0);
  }
    
  // Get ready for next iteration of loop
  delay(1000);
}

Proietti:
I am using millis instead of minutes in order to get a smoother fade.

This is the part of what you first posted that prompted my comments.

void SetLed(int timenowMillis, 
            int ledPin, 
            int start, 
            int fadein, 
            int period, 
            int fadeout, 
            int setPWM)
{
  if (timenowMillis > start && timenowMillis <= start + fadein)
  {
    analogWrite(ledPin, map(timenowMillis - start, 0, fadein, 0, setPWM)); //Fade in state
  }
  if (timenowMillis > start + fadein && timenowMillis <= start + period - fadeout)       
  {
    analogWrite(ledPin, setPWM);                                      //Holding state
  }
  if (timenowMillis > start + period - fadeout && timenowMillis <= start + period)
  {
    analogWrite(ledPin, map(timenowMillis - start + period - fadeout, 0, fadeout, setPWM, 0)); //Fade out state
  }
}

Human senses are not all linear. Perceived brightness change is by percent of the total, a significant change to a dim light won’t be noticed on a bright light. If you fade-step 10% it will be noticed right then but 5% or less will be hard to impossible to catch. Fading down means big steps to tiny ones.