keeping the Arduino in a function for X time

unsigned long startCollecting = millis();
while(millis() - startCollecting < collectionTime)
{
   // get some data from the sensor
   // do something with the data
}

That will spin, doing useful stuff for however long collectionTime is set to.