Add to the bottom of the website time statistics that the website has been running stably for X year, month, and day¶
Original link: https://www.itylq.com/js-and-php-for-runtime.html
Release date: 2022-10-11 Migration time: 2026-03-21
It can be implemented with js or php.
js code:
<script>
/*Website building time*/
function show_date_time() {
window.setTimeout("show_date_time()", 1e3);
varBirthDay = new
Date("2022/09/20"),
today = new Date,
timeold = today.getTime() -
BirthdayDay.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 + "day" + hrsold + "hour" + minsold + "minute" + seconds + "second";
}
show_date_time();
</script>
Copy and paste the above code into the desired location in the footer.php file, such as placing it in the middle of a
or .php code:
//Website running time
function show_runtime() {
//The time this site was created
$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;
}
}
Insert the above code into the functions.php file, then go to the location where it needs to be displayed in the footer.php file and add the following import code:
<div>The website is running stably: <span style="color:#3274ff;"><?php $uptime=show_runtime();echo $uptime['years']; ?>Year<?php echo $uptime['days']; ?>Day<?php echo $uptime['hours']; ?>Hour<?php echo $uptime['minutes']; ?>Minute<?php echo $uptime['seconds']; ?>Seconds</span></div>
Both methods have advantages and disadvantages. The advantage of the js method is that the number of seconds counted can change in real time, but the disadvantage is that one more js has to be run on the web page. The php method is just the opposite.
This article was moved from WordPress to MkDocs