很多朋友喜欢在WordPress主题开发的时候自己网站底部加一个网站已运行多少天多少秒.
一方面能够鞭策自己坚持更新网站.
一方面能够告诉朋友网站的年龄.
如何实现呢? 2个方案.
对了,本段代码一般添加在你的网站的 footer.php里面,也就是你的WordPress主题即网站底部;
本段代码采用js实现,所以不是WordPress的网站也可以使用.
<span id="runtime_span"></span> <script type="text/javascript">function show_runtime(){window.setTimeout("show_runtime()",1000);X=new Date("02/02/2019 12:20: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);runtime_span.innerHTML="网站已运行"+A+"天"+B+"小时"+C+"分"+D+"秒"}show_runtime();</script>
注意事项:
1. <span id=”runtime_span”></span> 这个必须存在;
2. X=new Date(“02/02/2019 12:20:00”); 这里面的时间请改为你的建站初始时间;
网站已运行:<span id="run_time" style="color: black;"></span> <script> function runTime() { var d = new Date(), str = ''; BirthDay = new Date("2019-02-25"); today = new Date(); timeold = (today.getTime() - BirthDay.getTime()); sectimeold = timeold / 1000 secondsold = Math.floor(sectimeold); msPerDay = 24 * 60 * 60 * 1000 msPerYear = 365 * 24 * 60 * 60 * 1000 e_daysold = timeold / msPerDay e_yearsold = timeold / msPerYear daysold = Math.floor(e_daysold); yearsold = Math.floor(e_yearsold); //str = yearsold + "年"; str += daysold + "天"; str += d.getHours() + '时'; str += d.getMinutes() + '分'; str += d.getSeconds() + '秒'; return str; } setInterval(function () { $('#run_time').html(runTime()) }, 1000); </script>
注意事项:
1. BirthDay = new Date(“2019-02-25”); 里面的出生日期改为你自己的.