How to Set Time Delay in PHP ? Last Updated : 17 Apr, 2024 Comments Improve Suggest changes Like Article Like Report To set a time delay in PHP, you have several approaches depending on your requirements. The time delay is useful for various purposes like creating a pause in script execution or scheduling tasks. These are the following different approaches: Table of Content Using sleep() FunctionUsing usleep() FunctionUsing while loopUsing DateTime() ObjectUsing sleep() FunctionThe sleep() function pauses the execution of the script for a specified number of seconds. we are going to use that function for showing the time delaying between the printing the lines on the screen. Example: This example shows the use of sleep() function for time delay. PHP <?php echo "Start\n"; sleep(1); // Pauses script execution for 1 seconds echo "End\n"; ?> Output: StartEndUsing usleep() FunctionThe usleep() function pauses the execution in microseconds. we are going to use that function for showing the time delaying between the printing the lines on the screen. Example: This example shows the use of usleep() function for time delay. PHP <?php echo "Start\n"; usleep(500000); // Pauses script execution for 0.5 seconds echo "End\n"; ?> OutputStart End Using while loopUse a while loop with a time condition to create a delay. it will first print the line that is outside of the loop then after one second it will print the line inside the loop. Example: This example shows the implementation of the above-explained approach. PHP <?php echo "Start\n"; $start = time(); while (time() - $start < 1) { // Loop for 1 seconds } echo "End\n"; ?> OutputStart End Using DateTime() ObjectCreate a future timestamp and compare it to the current time. then create a loop that will execute after 1 second exactly. Example: This example shows the implementation of the above-explained approach. PHP <?php echo "Start\n"; $delay = new DateTime('+1 seconds'); while (new DateTime() < $delay) { // Loop until delay time is reached } echo "End\n"; ?> Output: StartEnd Comment More infoAdvertise with us Next Article How to Set Time Delay in PHP ? A anjugaeu01 Follow Improve Article Tags : PHP Similar Reads How to Set Time Delay in JavaScript? Delaying the execution of code is a fundamental technique that is commonly used in JavaScript for tasks like animations, API polling, or managing time intervals between actions. JavaScript provides several built-in methods to set time delays: setTimeout() and setInterval(). We can set time delay in 2 min read How to Start and Stop a Timer in PHP ? You can start and stop a timer in PHP using the microtime() function in PHP. The microtime() function is an inbuilt function in PHP which is used to return the current Unix timestamp with microseconds. In this article, you will learn the uses of the microtime() function. Syntax: microtime( $get_as_f 2 min read How to change the session timeout in PHP? In PHP, sessions are maintained to check if the user is active. When the user becomes inactive and the user forgets to logout from the web page, there is a chance of other users viewing the page causing security breach. By default, a session in PHP gets destroyed when the browser is closed. Session 2 min read How to Destroy Session After Some Time in PHP ? In PHP, we create sessions for the user who is logged in and make that user online till the user log out of that session. It can be done by clicking on the logout button or by destroying that session after a fixed time. By default the expiry time of any particular session that is created is 1440 se 3 min read PHP | date_time_set() Function The date_time_set() function is an inbuilt function in PHP which is used to sets the time. This function resets the current time of the DateTime object to a different time. Syntax: Procedural style: date_time_set( $object, $hour, $minute, $second, $microseconds ) Object oriented style: DateTime::set 2 min read Like