config - Proper way to set php max_execution_time

781

i try to set my execution time inphp, but seems like it doesn't work.

I have tried this.

<?php

function secondsToTime($s)
{
    $h = floor($s / 3600);
    $s -= $h * 3600;
    $m = floor($s / 60);
    $s -= $m * 60;
    return $h.':'.sprintf('%02d', $m).':'.sprintf('%02d', $s);
}
$a = microtime(true);

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

ini_set('max_execution_time', 3);
sleep(2);
echo 'oke';

sleep(3);
echo 'yes';
$b = microtime(true);

echo secondsToTime($b-$a);

?>

But when i try, it shows 11 seconds, instead of error or something.

why does my code doesn't error?? i set time to 6 secs. but my whole process is 11 secs?? Do i need to define it on myphp.ini too? Thank you.

276

Answer

Solution:

it's show me this Error

Fatal error: Maximum execution time of 6 seconds exceeded

You doesn't get this error because on your server "Display Error" is not on.

Do i need to define it on my php.ini too ?

No need to define it on php.ini too.you can set this dynamically like you already had.

301

Answer

Solution:

set_time_limit sets the maximum execution time in seconds. If set to zero, no time limit is imposed

set_time_limit(0);

By default, the maximum execution time for PHP scripts is set to 30 seconds. If a script runs for longer than 30 seconds, PHP stops the script and reports an error. You can control the amount of time PHP allows scripts to run by changing the max_execution_time directive in your php.ini file.

People are also looking for solutions to the problem: Create Dynamic fileupload api in php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.