根据 西狂部落阁 的文章提供的代码修改下样式,将代码放置在底部模板中或者你需要的地方(footer、sidebar 等等),我这里的模板是 index.php 中。修改好其中自定义的网站建成时间和相关文字,就可以直接运行了。当然你可以修改 sitetime 的 CSS ,让它更有效果。
注意代码中的 “” ‘’ 引号,复制后要确保是英文状态下的,否则会出错。
- <p>博客稳定运行<span style="color: orange;" id="sitetime"></span></p>
- <script language=javascript>
- function siteTime(){
- window.setTimeout("siteTime()", 1000);
- var seconds = 1000
- var minutes = seconds * 60
- var hours = minutes * 60
- var days = hours * 24
- var years = days * 365
- var today = new Date()
- var todayYear = today.getFullYear()
- var todayMonth = today.getMonth()
- var todayDate = today.getDate()
- var todayHour = today.getHours()
- var todayMinute = today.getMinutes()
- var todaySecond = today.getSeconds()
- /* 修改下面的t1数值为网站建成时间(年,月,日,时,分,秒)*/
- var t1 = Date.UTC(2013,11,9,0,0,0)
- var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond)
- var diff = t2-t1
- var diffYears = Math.floor(diff/years)
- var diffDays = Math.floor((diff/days)-diffYears*365)
- var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours)
- var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes)
- var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds)
- document.getElementById('sitetime').innerHTML = diffYears +' 年 '+ diffDays + ' 天 ' + diffHours + ' 小时 ' + diffMinutes + ' 分钟 ' + diffSeconds + ' 秒'
- }
- siteTime()
- </script>