无聊人x的小记

【技术总结】- 博客统计中的动态运行天数代码

Blog底部一直空着,时常是默认的主题配置,Power by xx ,Theme xx觉得无趣,于是在网上找找看底部foot上可以显示啥,找到了一个老哥显示的是blog建立的时间,觉得这个想法不错,搜索了一下原理,就把另外一个老哥分享的代码给拔过来了,自己再按照需求修改了一下,目前符合我Ghost Blog的需求

PHP 版本

<?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('2018-01-01 00:00:00');
$time = time() - $site_create_time;
$uptime = Sec2Time($time);
?>

This blog has been running for :<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>

JS版本

<script>
    function secondToDate(second) {
        if (!second) {
            return 0;
        }
        var time = new Array(0, 0, 0, 0, 0);
        if (second >= 365 * 24 * 3600) {
            time[0] = parseInt(second / (365 * 24 * 3600));
            second %= 365 * 24 * 3600;
        }
        if (second >= 24 * 3600) {
            time[1] = parseInt(second / (24 * 3600));
            second %= 24 * 3600;
        }
        if (second >= 3600) {
            time[2] = parseInt(second / 3600);
            second %= 3600;
        }
        if (second >= 60) {
            time[3] = parseInt(second / 60);
            second %= 60;
        }
        if (second > 0) {
            time[4] = second;
        }
        return time;
    }
</script>
<script type="text/javascript" language="javascript">
    function setTime() {
        // 博客创建时间秒数,时间格式中,月比较特殊,是从0开始的,所以想要显示5月,得写4才行,如下
        var create_time = Math.round(new Date(Date.UTC(2017, 12, 1, 0, 0, 0))
                .getTime() / 1000);
        // 当前时间秒数,增加时区的差异
        var timestamp = Math.round((new Date().getTime() + 8 * 60 * 60 * 1000) / 1000);
        currentTime = secondToDate((timestamp - create_time));
        currentTimeHtml = currentTime[0] + 'Year ' + currentTime[1] + 'Day '
                + currentTime[2] + 'Hour ' + currentTime[3] + 'Minute' + currentTime[4]
                + 'Second';
        document.getElementById("htmer_time").innerHTML = currentTimeHtml;
    }
    setInterval(setTime, 1000);
</script>


   This blog has been running for :<span id=" htmer_time" style="color: red;"></span>

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

评论