WordPress添加运行天数代码

By | 2020 年 5 月 29 日

使用方法:将1~43行内容放到网站的footer或header中,然后将44行代码插入统计代码当中或网站合适的位置即可


 <!-- 运行天数代码开始 --><?php
/**
 * 秒转时间,格式 年 月 日 时 分 秒
 * 
 * @author wangyupeng129@126.com
 * @param int $time
 * @return array|boolean
 */
// 设置时区 
date_default_timezone_set('Asia/Shanghai');
function Sec2Time($time){
    if(is_numeric($time)){
        $value = array(
                "years" => 0, "days" => 0, "hours" => 0,
                "minutes" => 0, "seconds" => 0,
        );
        if($time >= 31556926){
            $value["years"] = floor($time/31556926);
            $time = ($time%31556926);
        }
        if($time >= 86400){
            $value["days"] = floor($time/86400);
            $time = ($time%86400);
        }
        if($time >= 3600){
            $value["hours"] = floor($time/3600);
            $time = ($time%3600);
        }
        if($time >= 60){
            $value["minutes"] = floor($time/60);
            $time = ($time%60);
        }
        $value["seconds"] = floor($time);
        return (array) $value;
    }else{
        return (bool) FALSE;
    }
}
// 本站创建的时间
$site_create_time = strtotime('2005-06-23 00:00:00');
$time = time() - $site_create_time;
$uptime = Sec2Time($time);
?><!-- 运行天数代码结束 -->

以下代码放入统计代码中,或者合适的位置:

本站运行:<span style="color:red;"><?php echo $uptime['years']; ?>年<?php echo $uptime['days']; ?>天<?php echo $uptime['hours']; ?>小时<?php echo $uptime['minutes']; ?>分<?php echo $uptime['seconds']; ?>秒</span>
<!--以下为计时功能代码-->
            <script language="javascript">
            function tick() {
                var years,days,hours, minutes, seconds;
                var openday = new Date('2005/06/23 00:00');    //这里填写建站时间
                var today = new Date(); //获取系统当前时间
                var total = (today.getTime()-openday.getTime())/1000;
                years=Math.floor(total/31536000);
                total=total-years*31536000;
                days=Math.floor(total/86400);
                total=total-days*86400;
                hours=Math.floor(total/3600);
                total=total-hours*3600;
                minutes=Math.floor(total/60);
                total=total-minutes*60;
                seconds=Math.floor(total);
                timeString = "网站运行:"+years+"年"+days+"天"+hours+"时"+minutes+"分"+seconds+"秒";
                document.getElementById("Clock").innerHTML = timeString;
                window.setTimeout("tick();", 1000);
            }
            window.onload = tick;
            </script>
            <span id="Clock"></span>
            <!--以上为计时功能代码-->