网站添加本站已运行XX天的统计代码

    选择打赏方式

傲世发现有许多的站长都喜欢给自己的网站上加一个本站已运行时间的显示,那么该如何做到呢?今天傲世就给大家分享几种方法,希望能帮到你。

js实现网站已运行XX天的代码

示例1:

<script language = "JavaScript"type = "text/javascript" >
    //添加网站的上线时间
    var urodz = new Date("11/01/2019");
    var now = new Date();
    var ile = now.getTime() - urodz.getTime();
    var dni = Math.floor(ile / (1000 * 60 * 60 * 24));
    document.write(dni+"天");
</script>

代码运行结果:1天

注:

1、此示例只能计算出开始时间到现在相距多天,但是代码逻辑简单

2、将 11/01/2019 修改成自己网站的上线时间即可;

示例2:

<span id="span"></span>
<script type="text/javascript">
    function runtime(){
        // 初始时间,日/月/年 时:分:秒
        X = new Date("11/01/2019 0:0:00");
        Y = new Date();
        T = (Y.getTime()-X.getTime());
        M = 24*60*60*1000;
        a = T/M;
        A = Math.floor(a);
        b = (a-A)*24;
        B = Math.floor(b);
        c = (b-B)*60;
        C = Math.floor((b-B)*60);
        D = Math.floor((c-C)*60);
        //信息写入到DIV中
        span.innerHTML = "本站勉强运行: "+A+"天"+B+"小时"+C+"分"+D+"秒"
    }
    setInterval(runtime, 1000);
</script>

运行结果:

本站勉强运行: 1天0小时0分0秒

注:

1、此代码输出的时间,其秒数是动态变化的

2、将 11/01/2019 0:0:00 修改成自己网站的上线时间即可

php实现网站已运行XX天的代码

示例:

<?php
function Sec2Time($time){
    //设置时区,如果需要去掉注释即可
    //date_default_timezone_set('Asia/Shanghai');
    $stime = strtotime($time);
    $times = time() - $stime;
    if(is_numeric($times)){
        $value = array(
            "years" => 0, 
            "days" => 0, 
            "hours" => 0,
            "minutes" => 0, 
            "seconds" => 0,
        );
        if($times >= 31556926){
            $value["years"] = floor($times / 31556926);
            $times = ($times % 31556926);
        }
        if($times >= 86400){
            $value["days"] = floor($times / 86400);
            $times = ($times % 86400);
        }
        if($times >= 3600){
            $value["hours"] = floor($times / 3600);
            $times = ($times % 3600);
        }
        if($times >= 60){
            $value["minutes"] = floor($times / 60);
            $times = ($times % 60);
        }
        $value["seconds"] = floor($times);
        return (array) $value;
    }else{
        return (bool) FALSE;
    }
}
//可以修改成自己网站的初始时间
$time = Sec2Time('2019-11-01 00:00:00');
echo $time['years'].'年'.$time['days'].'天'.$time['hours'].'小时';
// $time['minutes'] 分
// $time['seconds'] 秒
?>

运行结果:

0年1天0小时

注:

1、此PHP代码,会输出年月日时分秒,但时间段不会动态变化

2、虽然此代码可以实现,网站运行多长时间的功能,但傲世还是不建议大家使用此代码

版权声明:若无特殊注明,本文为《傲世》原创,转载请保留文章出处。
本文链接:https://www.recho.cn/79.html
正文到此结束

热门推荐

发表吐槽

你肿么看?

你还可以输入 250 / 250 个字

呵呵 哈哈 吐舌 开心 笑眼 可怜 乖 啊 你懂得 不高兴 生气 汗 黑线 哭 真棒 阴险 鄙视 酷 滑稽 纳尼 疑问 委屈 惊讶 勉强

评论信息框
可使用QQ号实时获取头像自动填写

私密评论

吃奶的力气提交吐槽中...

已有2条吐槽

匿名

2022-09-02 15:58 俄罗斯 CZ88.NET
I think that what you composed was actually very logical.
But, what about this? what if you wrote a catchier title?

I am not saying your information isn't solid, however
suppose you added a post title that makes people desire
more? I mean 网站添加本站已运行XX天的统计代码 - 阅客网 is
kinda boring. You should glance at Yahoo's home page and watch how they write post titles to
get people to click. You might add a related video or a pic or two to grab readers excited about what you've got to say.
In my opinion, it might bring your posts a little bit more interesting.
  Windows 10 x64    Google Chrome 103.0.0.0

小傲世

2022-09-02 16:01 广东省深圳市电信
@匿名:Thanks for your suggestion, I will consider it.
  Windows 10 x64    Google Chrome 104.0.5112.102