PHP | time_sleep_until()函数

PHP中的time_sleep_till()函数是一个内置函数,用于将当前脚本的执行延迟到指定的时间。 time_sleep_till()函数接受timestamp作为参数,这个timestamp表示脚本应该何时唤醒。 time_sleep_till()函数在成功时返回TRUE,在失败时返回FALSE。

null

语法:

time_sleep_until(timestamp)

使用的参数: PHP中的time_sleep_till()函数接受一个参数 时间戳 。这是一个强制参数,用于指定唤醒时间。

返回值: 成功时返回TRUE,失败时返回FALSE。

错误和例外 :

  1. 如果指定的时间戳是过去的,此函数将生成E_警告。
  2. 所有信号在脚本唤醒后发送。
  3. 如果指定的数字为负数,此函数将引发错误。

例如:

Input : echo date('h:i:s');
        time_sleep_until(time()+5);
        echo date('h:i:s'); 
Output: 07:23:26
        07:23:31

Input : echo date('h:i:s');
        time_sleep_until(time()+ rand(1, 3));
        echo date('h:i:s');
Output : 07:21:55
         07:21:57

下面的程序演示了time_sleep_until()函数:

方案1 :

<?php
// displaying time
echo date ( 'h:i:s' );
// delaying execution of script for 5 seconds
time_sleep_until(time()+5);
// displaying time again
echo ( "" );
echo date ( 'h:i:s' );
?>


输出:

06:50:04
06:50:08

方案2 :

<?php
// displaying time
echo date ( 'h:i:s' );
// using rand() function to randomly choose a
// value and delay execution of the script
time_sleep_until(time()+ rand(1, 3));
// displaying time again
echo ( "" );
echo date ( 'h:i:s' );
?>


输出:

06:50:14
06:50:15

方案3 :

<?php
// delaying execution of script with negative time
time_sleep_until(time()-2);
// displaying time again
echo ( "" );
echo date ( 'h:i:s' );
?>


输出:

false

参考: http://php.net/manual/en/function.time-sleep-until.php

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享