教程
网站添加加载时间
900

网页添加加载时间,适用于一切网站,放在合适的位置即可,今天就对于我的这个博客系统来讲来个简单教程。

代码食用

首页在主题下的 functions.php 中加入以下代码

/*
* 加载时间
* @return bool
*/
function timer_start() {
global $timestart;
$mtime     = explode( ' ', microtime() );
$timestart = $mtime[1] + $mtime[0];
return true;
}
timer_start();
function timer_stop( $display = 0, $precision = 3 ) {
global $timestart, $timeend;
$mtime     = explode( ' ', microtime() );
$timeend   = $mtime[1] + $mtime[0];
$timetotal = number_format( $timeend - $timestart, $precision );
$r         = $timetotal < 1 ? $timetotal * 1000 . " ms" : $timetotal . " s";
if ( $display ) {
    echo $r;
}
return $r;}

然后在要显示的地方(一般是在网站底部主题foot.php文件,根据你的实际情况)调用即可:

加载耗时:<?php echo timer_stop();?>

代码效果


  • 上一篇
  • 下一篇
  • 添加评论
    welcome to qiangzai blog