Typecho添加显示建站运行天数代码
1年前
1.将代码放入functions.php
一般在主题根目录:网站/usr/themes/主题,修改一下你自己的网站时间
// 设置时区
date_default_timezone_set('Asia/Shanghai');
/**
* 秒转时间,格式 年 月 日 时 分 秒
*
*/
function getBuildTime() {
// 在下面按格式输入本站创建的时间
$site_create_time = strtotime('2019-06-23 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);
echo '<span class="btime">'.$value['years'].
'年'.$value['days'].
'天'.$value['hours'].
'小时'.$value['minutes'].
'分</span>';
} else {
echo '';
}
}
2.修改 footer.php在页脚选择合适的位置加入:
<?php getBuildTime(); ?>
另一种方式
首先我们先在文章想要显示的地方加上以下代码,一般我们添加在文章的版权部分,有的主题版权部分可能在functions.php中,也可能在footer.php中
<p id="time">Blog 的站点正在加载数据.....</p>
然后我们在到footer.php文件中添加JS
<script type="text/javascript">
var colorStr="";
var randomArr=['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'];
for(var i=0;i<6;i++){
colorStr+=randomArr[Math.ceil(Math.random()*(15-0)+0)];
}
var now =new Date();
function StorageTime() {
var grt= new Date("03/02/2020 00:00:00");//时间格式:月/日/年 时/分/秒
now.setTime(now.getTime()+250);
years = Math.floor((now - grt ) / 1000 / 60 / 60 / 24 /365);
days = Math.floor((now - grt ) / 1000 / 60 / 60 / 24 - (years * 365));
hours = Math.floor((now - grt ) / 1000 / 60 / 60 - (24 * Math.floor((now - grt ) / 1000 / 60 / 60 / 24)));
if(String(hours).length ==1 ){hours = "0" + hours;}
minutes = Math.floor((now - grt ) / 1000 /60 - (24 * 60 * Math.floor((now - grt ) / 1000 / 60 / 60 / 24)) - (60 * hours));
if(String(minutes).length ==1 ){minutes = "0" + minutes;}
seconds = Math.floor((now - grt ) / 1000 - (24 * 60 * 60 * Math.floor((now - grt ) / 1000 / 60 / 60 / 24)) - (60 * 60 * hours) - (60 * minutes));
if(String(seconds).length ==1 ){seconds = "0" + seconds;}
if(years!=0){var outputtime="本站已安全存活<span>"+years+"</span>年"+"<span>"+days+"</span>天"+"<span>"+hours+"</span>时"+"<span>"+minutes+"</span>分"+"<span>"+seconds+"</span>秒";}else{var outputtime="本站已安全存活<span>"+days+"</span>天"+"<span>"+hours+"</span>时"+"<span>"+minutes+"</span>分"+"<span>"+seconds+"</span>秒";}
document.getElementById("time").style.color="#"+colorStr;
document.getElementById("time").innerHTML = outputtime;
}
setInterval("StorageTime()",250);
</script>
最后在header.php里加个CSS样式就大功告成了
<style>
#time{
text-shadow: 0px 0px 3px blue;
text-align: center;
}
#time span{
color: red;
padding: 0 8px;
}
</style>
文章来源于: http://www.manongjc.com/detail/57-prkiggiwiiotelo.html和https://owo-bo.cn/code/chapter-002.html?replyTo=219