给typecho博客添加网站加载时间

建站折腾
4361 5

想及时了解自己的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();?>

就是这么简单,快点去自己的 typecho博客出实践吧!

最后更新 2019-10-13
评论 ( 5 )
OωO
隐私评论
  1. 烟尘

    <?php echo timer_stop();?
    这里少了一个>,我说呢→_→

    5年前上海市浦东新区回复
  2. 已拿走,谢谢博主 ::twemoji:tongue::

    5年前广东省深圳市回复
  3. 来串串门

    5年前广东省广州市回复
  4. 来逛逛 ::twemoji:tiaomei:: 嘿嘿 ::twemoji:cool::

    5年前广东省惠州市回复
  5. 不错 ::twemoji:smile::

    5年前广东省深圳市回复