Script timing out

I have a script that times out after sometime,
despite having set_time_limit(0); on top,
what could be the cause?

On Apache? From the command line or through a browser?

Yes on Apache, through browser.