millis() and Serial.available() Issue

Hi everyone ,

I am trying to make a code to work for X seconds , when I dont use :

if (Serial.available() > 0)

It works perfectly , but I want to time to be started after the if (Serial.available() > 0), but it does not work.

The working code is :

        time = millis();

	if(time < 9000) {
	
	  if (Serial.available() > 0)
	  {

            //MY CODE
        }
          }

but since i want the millis() to start after the user CLICK the button Serial.available() > 0, because the number 9000 will be a variable that the user enter in the PHP side , i do :

        time = millis();

	
	
	  if (Serial.available() > 0)
	  {
      
           if(time < 9000) {

            //MY CODE
        }
          }

but its not working unfortunately , any help would be very appreciated.

thanks alot

unsigned long StartTime = 0;
unsigned long Interval = 9000;

void loop() {
  if (Serial.available() > 0) {
    StartTime = millis();   // Start the timed code
  }

  if (StartTime != 0 && millis() - StartTime < Interval) {
    // This will repeat for Interval milliseconds
  } else {
    StartTime = 0;  // Turn off the timer.
  }
}

millis() counts milliseconds from power on, so 9 seconds in millis() returns a value > 9000. Use an interval instead, as demonstrated in #1.

Its working great , thank you alot guys