Hi,

Please consider the below code.

<html>
<body>

<?php

$time1 = localtime();
echo $time1[0];


echo("<br /><br />");


/* HERE SOME CODES RELATED TO CERTAIN FUNCTIONS LIKE DISPLAYING OF SOME IMAGE ETC. IS EXECUTED. APPROXIMATELY IT TAKES 10 SECONDS FOR THE COMPLETE EXECUTION OF THESE FUNCTIONS. SINCE IT IS IRRELEVANT TO MY QUESTION, I REMOVED THEM FOR THE SAKE OF KEEPING THIS PIECE OF CODE SIMPLE.   */


echo("<br /><br />");


$time1 = localtime();
echo $time1[0];


?>

</body>
</html>

OK, now I get the following output when I execute the above code:

48

**** THE OUTPUT OF THE FUNCTIONS LIKE LOADING CERTAIN IMAGES.  NOTE: IT TAKES APPROX. 10 SECONDS FOR THIS OUTPUT TO COMPLETELY APPEAR ON THE SCREEN

48

Now, my question is:
In the beginning and ending of the code, I echoed localtime()[0] which displays the 'seconds' value of the current time. But in the middle of the code, I executed certain functions which took approx. 10 seconds to get executed. Then how come localtime()[0] present in the ending outputted the same value which it outputted in the beginning??

Dani AI

Generated

Short answer: nothing mystical was wrong with PHP — the apparent 10‑second gap was happening outside the server-side timing you were measuring. As suggested and confirmed by adding a sleep, the server either completed the PHP work in under a second (so the seconds value didn’t change) or the output was buffered and not sent to the browser until later. The long wait the browser shows is often caused by client-side image/network loading or HTTP buffering, not the PHP execution time.

Key points to check and why they matter:

  • PHP and the webserver can buffer output, so early echo calls may not reach the browser immediately. See ob_flush/flush and PHP’s output buffering settings for details (ob_flush, flush, output_buffering ini).
  • Network, browser rendering, compression (gzip/mod_deflate) and proxies can further delay visible output even after the server sends it. Chunked/streaming behaviour is not guaranteed unless all layers allow it.

How to prove and fix it:

  • Measure real server time with high resolution using microtime() and compute the elapsed seconds to see actual execution cost (microtime).
  • If you need to show progress to users, use deliberate server-side flushing plus disabling gzip and proxy buffering, or better: use client-side techniques (AJAX polling, Server‑Sent Events or WebSockets) so the browser receives progress independently of HTTP buffering.

Caution: calling flush() is not a universal solution — it can be ignored by server/proxy/browser settings. For reliable progress indicators, instrument server timing in logs and use a client-driven update channel.

Recommended Answers

All 2 Replies

I suspect that it isn't taking anything like 10 seconds to execute the functions that you didn't show. That's a long time on a fast machine. Put a sleep command in just after all the code that you didn't show and I'll bet that you'll see a difference in the two values. The time until it displays might be quite long because of network transmission time or the time it takes the browser to deal with it but it may not be anywhere near that amount of time on the server. If you want to see how much time it is actually taking use / display the microtime before and after.

I suspect that it isn't taking anything like 10 seconds to execute the functions that you didn't show. That's a long time on a fast machine. Put a sleep command in just after all the code that you didn't show and I'll bet that you'll see a difference in the two values. The time until it displays might be quite long because of network transmission time or the time it takes the browser to deal with it but it may not be anywhere near that amount of time on the server. If you want to see how much time it is actually taking use / display the microtime before and after.

Ok, it seems you are right! I added that sleep command and got the correct time difference this time.

Thanks a lot for your time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.