Problem running a function for a given amount of time.

Hi! Im currently trying to complete a project which, when a button is pressed, runs a function for a given amount of time. The time should be constant, no matter when the button is pressed. I am seeking guidance on how to do this, since i have done a fair amout of research, yet i have come up fairly empty-handed.
Here is my code at the time of writing this:

const int relayPin = 9;  
const int relayPin2 = 10; 
const int relayPin3 = 11; 
const int inputPin = 3;
const int buttonPin = 2;

int time1 = 0;

int state = HIGH;
int reading; 
int previous = LOW; 

long time = 0;
long debounce = 200; 
void setup() {
pinMode(relayPin, OUTPUT); 
pinMode(relayPin2, OUTPUT); 
pinMode(relayPin3, OUTPUT); 
pinMode(inputPin, INPUT);  
pinMode(buttonPin, OUTPUT); 

digitalWrite(relayPin, HIGH); 
digitalWrite(relayPin2, HIGH); 
digitalWrite(relayPin3, HIGH); 

Serial.begin(9600); 

digitalWrite(buttonPin, HIGH);
}
void loop() { 
reading = digitalRead(inputPin);

if(reading == HIGH && previous == LOW && millis() - time > debounce){
  if(state == HIGH){
    state = LOW; 
  }
  else {
    state = HIGH;
  }
  time = millis();
 digitalWrite(relayPin, state);
}
  
  previous = reading;
}

void kartoffel(){
Serial.println(time);
}

I haven't written the function(void kartoffel()) which i wish to run for the given amount of time, since i'd like to know if and how, this is possible. Thanks for your help!

When you detect that the button has become pressed set a boolean variable to true and save the value of millis() as the start time

Later in loop() test the boolean and if it is true and if the required run period has not elapsed based on the start time and current time then call the function else if the period has elapsed then set the boolean to false and do not call the function. Note that the function must not contain any blocking code

Function run till they return. You can get the time on entry to the function, then wait until a certain time
has elapsed before electing to return. In other words the function blocks.

However this is probably the wrong thing to do, and I strongly suspect the xyproblem here.

What are you trying to achieve using this mechanism? Have you looked at the BlinkWithoutDelay
example and understood how time is meant to be managed in the Arduino's event loop model?