PHP 页面生成计时器返回的数字真的很高吗? (显然超过 10 亿秒)
PHP page generation timer is returning really high numbers? (1billion+ seconds apparently)
在我的 PHP 项目中,我正在尝试为页面生成计时。使用 http://blog.alastair.pro/2013/01/18/php-page-generation-time/ 中的脚本,我创建了一个函数,我在 footer.php 和其他没有页脚的杂项 PHP 文件中调用。据我所知,这应该有效:
function timer($type)
{
$totalTime = round((microtime(TRUE) - $_SERVER['REQUEST_TIME_FLOAT']), 4);
if (is_int($type)) {
if ($type == TYPE_TIMER_COMMENT) {
return "<!-- Page generated in " . $totalTime . " seconds. -->";
} elseif ($type == TYPE_TIMER_PLAINTEXT) {
return "Page generated in " . $totalTime . " seconds.";
}
} else {
header('location:./error/type.html');
die();
}
}
不幸的是,事实并非如此。它输出巨大的数字:
Page generated in 1428979311.8916 seconds.
Page generated in 1428979357.1691 seconds.
Page generated in 1428979346.8255 seconds.
等等等等
我完全不知道这里发生了什么。我是这样称呼它的:
if (Config::DEBUG)
echo ('<small>' . timer(TYPE_TIMER_PLAINTEXT) . '</small>');
TYPE_TIMER_PLAINTEXT 和 TYPE_TIMER_COMMENT 是在 functions.php 内部声明的常量,但我看不出这会如何影响函数的作用。对这里发生的事情有什么想法吗?
您得到的数字看起来很像您从 microtime(true) - 0
中得到的数字。这意味着 $_SERVER['REQUEST_TIME_FLOAT'] = 0
(或者至少当你减去它时表现得像零。)
基于 this note in the PHP documentation for microtime,我假设您的 PHP 版本是 < 5.4,并且 'REQUEST_TIME_FLOAT' 不在 $_SERVER 中。如果您的 PHP 版本是 >=5.4,那么我同意它看起来应该可以工作。
As of PHP 5.4.0, REQUEST_TIME_FLOAT is available in the $_SERVER superglobal array. It contains the timestamp of the start of the request with microsecond precision.
更新:我通过将 PHP 更新到比 5.4.0 更新的版本来解决问题。 $_SERVER['REQUEST_TIME_FLOAT'] 仅在 5.4.0 中引入,因此 PHP 5.3.3 造成了问题。
在我的 PHP 项目中,我正在尝试为页面生成计时。使用 http://blog.alastair.pro/2013/01/18/php-page-generation-time/ 中的脚本,我创建了一个函数,我在 footer.php 和其他没有页脚的杂项 PHP 文件中调用。据我所知,这应该有效:
function timer($type)
{
$totalTime = round((microtime(TRUE) - $_SERVER['REQUEST_TIME_FLOAT']), 4);
if (is_int($type)) {
if ($type == TYPE_TIMER_COMMENT) {
return "<!-- Page generated in " . $totalTime . " seconds. -->";
} elseif ($type == TYPE_TIMER_PLAINTEXT) {
return "Page generated in " . $totalTime . " seconds.";
}
} else {
header('location:./error/type.html');
die();
}
}
不幸的是,事实并非如此。它输出巨大的数字:
Page generated in 1428979311.8916 seconds.
Page generated in 1428979357.1691 seconds.
Page generated in 1428979346.8255 seconds.
等等等等
我完全不知道这里发生了什么。我是这样称呼它的:
if (Config::DEBUG)
echo ('<small>' . timer(TYPE_TIMER_PLAINTEXT) . '</small>');
TYPE_TIMER_PLAINTEXT 和 TYPE_TIMER_COMMENT 是在 functions.php 内部声明的常量,但我看不出这会如何影响函数的作用。对这里发生的事情有什么想法吗?
您得到的数字看起来很像您从 microtime(true) - 0
中得到的数字。这意味着 $_SERVER['REQUEST_TIME_FLOAT'] = 0
(或者至少当你减去它时表现得像零。)
基于 this note in the PHP documentation for microtime,我假设您的 PHP 版本是 < 5.4,并且 'REQUEST_TIME_FLOAT' 不在 $_SERVER 中。如果您的 PHP 版本是 >=5.4,那么我同意它看起来应该可以工作。
As of PHP 5.4.0, REQUEST_TIME_FLOAT is available in the $_SERVER superglobal array. It contains the timestamp of the start of the request with microsecond precision.
更新:我通过将 PHP 更新到比 5.4.0 更新的版本来解决问题。 $_SERVER['REQUEST_TIME_FLOAT'] 仅在 5.4.0 中引入,因此 PHP 5.3.3 造成了问题。