Need help adding a countdown function

I am looking for some help with a timer to control a relay. I have a 12V to 120V inverter in my work truck to charge my cordless tool batteries. I have found some code(thank you viralscience) that allows you to push a button to toggle on a relay. What I would like to do is change the code so that when you push the button it toggles the relay on for 2hrs and then shuts it off. This is so when I turn the inverter on it allows for enough time to charge the tool batteries but then automatically shuts off the inverter to save battery life. I'm using an arduino uno and a 4 channel relay board.

//Viral Science Viral Science - The home of Creativity - YouTube
//Push Button Relay Control
int pbuttonPin = 2;
int relayPin = 10;
int pbuttonPin2 = 3;
int relayPin2 = 11;

int val = 0;
int lightON = 1;
int pushed = 0;

int val2 = 0;
int lightON2 = 1;
int pushed2 = 0;

void setup() {
Serial.begin(9600);
pinMode(pbuttonPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
pinMode(pbuttonPin2, INPUT_PULLUP);
pinMode(relayPin2, OUTPUT);

}

void loop() {
val = digitalRead(pbuttonPin);
val2 = digitalRead(pbuttonPin2);

//Relay 1
if(val == HIGH && lightON == LOW){

pushed = 1-pushed;
delay(100);
}
lightON = val;

if(pushed == HIGH){
Serial.println("Light ON");
digitalWrite(relayPin, LOW);

}else{
Serial.println("Light OFF");
digitalWrite(relayPin, HIGH);

}

//Relay 2

if(val2 == HIGH && lightON2 == LOW){

pushed2 = 1-pushed2;
delay(100);
}

lightON2 = val2;

if(pushed2 == HIGH){
Serial.println("Light ON");
digitalWrite(relayPin2, LOW);

}else{
Serial.println("Light OFF");
digitalWrite(relayPin2, HIGH);

}

delay(100);
}

Start by following the advice given in Read this before posting a programming question

You want to combine the State Change Detection example code with the Blink Without Delay example code to get something like this

//Viral Science www.youtube.com/c/viralscience
//Push Button Relay Control
const int pbuttonPin = 2;
const int relayPin = 10;

bool relayOn = false;

unsigned long relayStartTime;
unsigned long relayOnDuration = 1000UL * 60 * 120;  // 120 minutes

int previousButtonState;

void setup() {
  Serial.begin(9600);
  pinMode(pbuttonPin, INPUT_PULLUP);
  pinMode(relayPin, OUTPUT);
  previousButtonState = digitalRead(pbuttonPin);
}

void loop() {
  int buttonState = digitalRead(pbuttonPin);

  // check runtime
  if (relayOn == true) {
    // check if we have been on long enough
    if (millis() - relayStartTime >= relayOnDuration) {
      Serial.println("Relay OFF");
      digitalWrite(relayPin, HIGH);
      relayOn = false;
    }
  }

  // check button press
  if (buttonState != previousButtonState ) {
    if ( buttonState == LOW ) {
      // button just got pushed so either turn on the relay
      // and start the timer or turn off the relay

      if ( relayOn == false ) {
        Serial.println("Relay ON");
        digitalWrite(relayPin, LOW);
        relayOn = true;
        relayStartTime = millis();
      }
      else {
        Serial.println("Relay OFF");
        digitalWrite(relayPin, HIGH);
        relayOn = false;
      }
    }
    delay(50);  // debounce
  }
  previousButtonState = buttonState;
}

Thank you so much for your help. Can you help me understand why the relay is on by default when it 1st starts up? Is there a way to prevent this from happening? It seems like I have to push the button once or twice before it starts working properly. After that is perfect

Can you help me understand why the relay is on by default when it 1st starts up?

Your relay is active LOW. That is you use digitalWrite(relayPin, LOW) to turn on the relay.

At startup, the out put pins default to LOW, so in your case, the relay will be on. You can change this by placing this code in setup() where you digitalWrite the HIGH state you want before setting the pinMode.

digitalWrite(x, HIGH);
pinMode(x, OUTPUT);

That did the trick, thank you again.