<?php
//时间函数
function timer()
{
$_time = explode( " ", microtime());
$_usec = (double)$_time[0];
$_sec = (double)$_time[1];
return $_timer = $_usec + $_sec;
}
$start_time = timer();
//你的代码$used_time = $end_time - $start_time;
echo $used_time;
?>

解决方案 »

  1.   

    晕,写错了在$used_time = $end_time - $start_time;前加个$end_time = timer();
      

  2.   

    这个放在一个<?php xxx ?>中,是不是是不是只能计算这一段的时间,如何计算整个php文件的执行时间呢?
      

  3.   

    嘿嘿,我自己弄了个计算时间滴,不知道合理不<?php
    $timer=new timer;
    $timer->start();
    $con=odbc_connect("MySql_DB","sa","658006",SQL_CUR_USE_ODBC) or die ("数据库连接出错");
    odbc_autocommit($con,On);
    $sql="Select * from mm order by id desc";
    $rs=odbc_exec($con,$sql);
    while(odbc_fetch_row($rs))
    {
    $no=odbc_result($rs,1);
    $name=odbc_result($rs,2);
    $pwd=odbc_result($rs,3);
    echo "编号:&nbsp;&nbsp;".$no."&nbsp;&nbsp;姓名:".$name."&nbsp;&nbsp;密码:".$pwd."<br>";
    }
    $mynum=odbc_num_rows($rs);
    echo "<br>一共有".$mynum."条数据<br>";
    $myfields=odbc_num_fields($rs);
    echo "<br>一共有".$myfields."列<br>";
    odbc_close($con);class timer
    {
    var $startTime=0;
    var $endTime=0;
    var $spendTime=0;
    function start()
    {
    $this->startTime=microtime();
    }
    function stop()
    {
    $this->endTime=microtime();
    }
    function spend()
    {
    if($this->spendTime)
    {
    return $this->spendTime;
    }
    else
    {
    $startMicro=substr($this->startTime,0,10);
    $startSecond=substr($this->startTime,11,10);
    $stopMicro=substr($this->endTime,0,10);
    $endSecond=substr($this->endTime,11,10);
    $start=doubleval($startMicro) + $startSecond;
    $stop=doubleval($stopMicro) + $endSecond;
    $this->spendTime=$stop - $start;
    return substr($this->spendTime,0,8)."妙";
    }
    }
    }
    $timer->stop();
    echo "花了好多时间,看看:".$timer->spend();;
    ?>