-1

I have to execute a piece of code in php and the code must be executed within 5 seconds.

if it is not executed within 5 seconds, a message should be 'echo'. Help me please.

splash21
  • 799
  • 4
  • 10

1 Answers1

0

get the start time before starting your code and end time after code in timestamp( which is in sec) and get difference after code end

$start_time = strtotime(date('d-M-Y g:i:s A'));

----
--- your piece of code
-----

$time_now = strtotime(date('d-M-Y g:i:s A'));

if($time_now-$start_time > 5)
{
     echo "Some thing ";
}
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
  • thanks but i dont want the execution time of the code, if the execution time is exceeding 5s, stop execution and echo a message – user3910487 Aug 06 '14 at 10:26
  • you can use this in between of your code after some statement – Satish Sharma Aug 06 '14 at 10:29
  • I'm have to ping a server and if the server does not response within 5s, an message should be appear. any solution for this – user3910487 Aug 06 '14 at 10:33
  • then you should use timeout. see some of links are there on google for it. https://www.google.co.in/?gws_rd=ssl#q=php%20ping%20a%20server%20with%20timeout – Satish Sharma Aug 06 '14 at 10:36