在網(wǎng)站上顯示已運行多少時(shí)間是一些站長(cháng)需要的功能,只需要加一段代碼即可實(shí)現。代碼來(lái)自六特網(wǎng)站,本站使用下面第三段。代碼貼到footer.php或者右側html小工具。
1.顯示的是運行的 天/小時(shí)/分/秒
<span id="timeDate">載入天數...</span><span id="times">載入時(shí)分秒...</span><script>// <![CDATA[var now = new Date(); function createtime(){ var grt= new Date("11/25/2013 21:51:00");//此處修改你的建站時(shí)間或者網(wǎng)站上線(xiàn)時(shí)間 now.setTime(now.getTime()+250); days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days); hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours); if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum); mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;} seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum); snum = Math.round(seconds); if(String(snum).length ==1 ){snum = "0" + snum;} document.getElementById("timeDate").innerHTML = "本站已安全運行"+dnum+"天"; document.getElementById("times").innerHTML = hnum + "小時(shí)" + mnum + "分" + snum + "秒"; } setInterval("createtime()",250);// ]]></script>
2.顯示的是運行的 天
本站已安全運行了:<span class="smalltxt"><script>// <![CDATA[BirthDay=new Date("11 25,2013");today=new Date();timeold=(today.getTime()-BirthDay.getTime());sectimeold=timeold/1000secondsold=Math.floor(sectimeold);msPerDay=24*60*60*1000e_daysold=timeold/msPerDaydaysold=Math.floor(e_daysold);document.write("<font color=red>"+daysold+"</font>天 !");// ]]></script></span>
3.顯示格式:已運行1 年 2 天 3 小時(shí) 4 分鐘 35 秒
<span id="sitetime"></span><script>// <![CDATA[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() /* Date.UTC() -- 返回date對象距世界標準時(shí)間(UTC)1970年1月1日午夜之間的毫秒數(時(shí)間戳) year - 作為date對象的年份,為4位年份值 month - 0-11之間的整數,做為date對象的月份 day - 1-31之間的整數,做為date對象的天數 hours - 0(午夜24點(diǎn))-23之間的整數,做為date對象的小時(shí)數 minutes - 0-59之間的整數,做為date對象的分鐘數 seconds - 0-59之間的整數,做為date對象的秒數 microseconds - 0-999之間的整數,做為date對象的毫秒數 */ var t1 = Date.UTC(2013,11,25,21,30,20) 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+" 小時(shí) "+diffMinutes+" 分鐘 "+diffSeconds+" 秒" } siteTime()// ]]></script>