耿俭

typecho程序添加网站加载时间方法
想要给typecho博客添加网站加载时间,其实使用简单的代码就可以实现。下面介绍一下如何给typecho添加加载时...
扫描右侧二维码阅读全文
24
2019/06

typecho程序添加网站加载时间方法

想要给typecho博客添加网站加载时间,其实使用简单的代码就可以实现。下面介绍一下如何给typecho添加加载时间。

在typecho博客主题文件 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;
}

然后在你需要显示时间的地方调用即可,一般在网站底部footer.php中引用:

<?php echo timer_stop();?

方法很简单,大家可以去试试。

最后修改:2019 年 06 月 24 日 08 : 40 AM

发表评论