【问题标题】:How to execute scripts at exact microtime in php [duplicate]如何在php中以精确的微时间执行脚本[重复]
【发布时间】:2018-03-11 19:23:31
【问题描述】:

我想在精确的微时间执行 php 脚本,或者如果它还没有执行,那么 1 分钟对我来说范围太大了。有这种可能吗?

【问题讨论】:

  • 你通常应该只用一分钟就可以搞定,为什么你需要这么精确?
  • 我不正常。
  • 我也可以把它推到一秒。
  • 你不能精确到秒,而且我认为你不需要它。如果你有一个 chron 工作,它会在这一分钟到来时立即发生,而不是在那一分钟内的任何时间。
  • 您可以执行cron,并等待一定的秒数...

标签: php cron


【解决方案1】:

假设您已经知道如何使用 cron,请将您的脚本设置为在您希望事情发生的精确时间之前的那一刻开始。将您想要的微时间值作为参数传递给脚本,并将其放在其余代码之前。

<?php
// get the start time from the argument passed to the script
$startTime = $argv[1];

// wait until it's time
while (microtime(true) < $startTime);

// do stuff

【讨论】:

    猜你喜欢
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    相关资源
    最近更新 更多