I need to determine how long a PHP function has been running so far.
What are some options to find out how long some PHP code takes to run?
I'm using zend framework.
Clock time can get using microtime() function. First use it before starts the script and then at the end of the script. Then using formula (End_time – Start_time). The mirotime() function returns time in seconds.
Remember, the max execution time of a PHP script is 30 seconds.
Replace #user# with your script user. Show activity on this post. The above code lists all php processes running in full, then checks to see if "my_script. php" is in the list of running processes, if not it runs the process and does not wait for the process to terminate to carry on doing what it was doing.
To increase the script execution time, you can use the following snippet at the top of your script. ini_set ( 'max_execution_time' , '300' ); In the above example, it would set the max_execution_time directive to 300 seconds.
Call microtime(true)
function to fetch current time with milisecond resolution.
<?php
$startTime = microtime(true);
/*stuff is going on*/
echo "Elapsed time is: ". (microtime(true) - $startTime) ." seconds";
microtime — Return current Unix timestamp with microseconds
<?php
$time = microtime(TRUE);
//...code here...
print (microtime(TRUE)-$time). ' seconds';
You could simply use PHP's standard timeout, and implement a shutdown function.
function say_goodbye() {
if (connection_status() == CONNECTION_TIMEOUT) {
... do your special processing to save the process state here
... then formulate a response for the browser
}
} // function say_goodbye()
register_shutdown_function("say_goodbye");
Note that you can set the shutdown function to accept parameters
EDIT
function say_goodbye($controller1,$controller2) {
if (connection_status() == CONNECTION_TIMEOUT) {
... do your special processing to save the process state here
... then formulate a response for the browser
}
} // function say_goodbye()
$ctl1 = new DBController();
$ctl2 = new OPController();
register_shutdown_function("say_goodbye",$ctl1,$ctl2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With