timer - need help

Hello, i have a issue with one project i am working.
i have a sensor on pin 2 that sends a signal each 20 mm (ir led and a black/white reflective sheet).
i am using it to calculate speed
i have gather portions of sketches, which works good for me which only one issue.
i need to start the time only when the switch changes to high or low.

cand someone help me?

// Constante:
const int buttonPin = 2; // pinul digital pentru senzorul de linie

// Variabile:
int buttonPushCounter = 0; // counter numar impulsuri (1 sau 0)
int buttonState = 0; // starea actuala a senzorului de linie (1 sau zero)
int lastButtonState = 0; // starea anterioara a senzorului de linie (1 sau zero)
unsigned long time;

void setup() {
// inititalizeaza intrarea pentru senzorul de linie:
pinMode(buttonPin, INPUT);
// initializeaza comunicatia seriala:
Serial.begin(115200);
Serial.println("");
Serial.println("Program test");
Serial.println("
");
}

void loop() {
// citeste intrarea senzorului de linie:
buttonState = digitalRead(buttonPin);

// compara starea butonului actuala cu cea anterioara
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.print("Deplasare [mm]: ");
Serial.println(buttonPushCounter20);
Serial.print("Timp : ");
~~ time = millis();~~
~~ //prints time since program started~~
~~ Serial.println(time/1000);~~
~~ Serial.print("Viteza [mm/s]: ");~~
~~ Serial.println((buttonPushCounter
10)/(time/1000));~~
~~ Serial.println("________________________");~~
~~ }~~
~~ // Delay a little bit to avoid bouncing~~
~~ delay(1);~~
~~ }~~
~~ // save the current state as the last state,~~
~~ //for next time through the loop~~
~~ lastButtonState = buttonState;~~
}