在PHP中获取脚本执行时间_检测PHP脚本执行时间

频道:PHP教程 日期:

在PHP中获取脚本执行时间_检测PHP脚本执行时间

//修改php的默认时区

date_default_timezone_get('PRC');
 $timer= new Timer();
$timer->start();
//这里放要执行的PHP脚本
$timer->stop();
 echo"执行该脚本用时".$timer->spent()."秒";
class Timer{
    private $startTime;
    private $stopTime;
    function __construct(){
        $this->startTime=0;
        $this->stopTime=0;
    }
    function start(){$this->startTime=microtime(true);
    }
    function stop(){$this->stopTime=microtime(true);
    }
    function spent(){return round(($this->stopTime - $this->startTime),4);
    } 
}