wordpress里通过php实现网站运行时间
07月30
很多时候我们看到有的网站底部会有一个本站已经运行多少天,这样的功能比较人性化,那么在WordPress里也可以通过php语言实现。
网站运行多长时间,这样的功能是如何实现的呢?
徐伟轩博客直接给出php代码
<?php
// 设置时区
date_default_timezone_set('Asia/Shanghai');
/**
* 秒转时间,格式 年 月 日 时 分 秒
*/
function getBuildTime(){
// 在下面按格式输入本站创建的时间
$site_create_time = strtotime('2020-03-07 22:15: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 '404发芽'.$value['years'].'年'.$value['days'].'天'.$value['hours'].'小时'.$value['minutes'].'分';
}else{
echo '';
}
调用方法,wordpress搭建的博客放在:footer.php中
本网站已经运行:<?php getBuildTime(); ?>
另外一种方法,网站里最常用的方法是,直接在网页中实现,我们也给出代码,供大家参考:
调用方法:
<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()
var t1 = Date.UTC(2019,12,12,16,19,00)
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>
以上徐伟轩给出的两种方式可以通过php实现网站运行时间的功能代码,希望会对大家有所帮助。


版权:
徐伟轩博客-爱写歌的程序员思考和科普的日常自媒体
本文链接:
https://letus.top/mblog/archives/20.html(转载时需注明本文出处及文章链接)
如无特别注明,本站内容为原创。
如需转载或刊登,请联系我们获得授权。