当我执行时,Microtime给我一个负面结果

时间:2013-03-07 14:17:13

标签: php time

运行脚本并确定我的执行时间。在计算时间时我得到负数。我希望它在执行时给我一个肯定的。

<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
echo "executed";

$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
$minutes = (int)($totaltime/60)-$hours*60;
echo "This page was created in ".$minutes." minute/s";

?>

1 个答案:

答案 0 :(得分:5)

microtime()作为字符串返回。尝试传入true以使其返回浮点值。这也会将您的代码缩短为:

$starttime = microtime(true);
echo "Executed";

$endtime = microtime(true);
$totaltime = $endtime - $starttime;
$minutes = intval(ceil(($endtime - $starttime) / 60));
echo "This page was created in " . $minutes . " minute(s)";
...

资源:http://www.php.net/microtime