js或者php均可以实现。
js:
<script>
/*建站时间*/
function show_date_time() {
window.setTimeout("show_date_time()", 1e3);
var BirthDay = new
Date("2022/09/20"),
today = new Date,
timeold = today.getTime() -
BirthDay.getTime(),
msPerDay = 864e5,
e_daysold = timeold / msPerDay,
daysold = Math.floor(e_daysold),
e_hrsold = 24 * (e_daysold -
daysold),
hrsold = Math.floor(e_hrsold),
e_minsold = 60 * (e_hrsold -
hrsold),
minsold = Math.floor(60 * (e_hrsold - hrsold)),
seconds = Math.floor(60 * (e_minsold - minsold));
span_dt_dt.innerHTML = daysold + "天" + hrsold + "小时" + minsold + "分" + seconds + "秒";
}
show_date_time();
</script>
复制以上代码粘贴到footer.php文件中需要的位置即可,比如放置到某个<div></div>、<span></span>中间。
php:
//网站运行时间
function show_runtime() {
// 本站创建的时间
$site_create_time = strtotime('2022-09-20 00:00:00');
$time = time() - $site_create_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;
}
}
将上述代码插入functions.php文件中,然后到footer.php文件中需要展示的位置,添加如下引入代码:
<div>网站已稳定运行:<span style="color:#3274ff;"><?php $uptime=show_runtime();echo $uptime['years']; ?>年<?php echo $uptime['days']; ?>天<?php echo $uptime['hours']; ?>小时<?php echo $uptime['minutes']; ?>分<?php echo $uptime['seconds']; ?>秒</span></div>
两种方式各有优缺点。js的方式,好处是统计的秒数可以实时变化,劣处是网页多要运行一个js。php的方式正好想反。
正文完