PHP 网站运行时间代码:年、天、小时、分钟、秒动态显示
以下是一个使用 PHP 编写的网站运行时间的代码,它将以年、天、小时、分钟和秒的形式显示运行时间,并且秒钟会动态更新:
<?php
// 网站的启动时间
$startTime = strtotime('2022-01-01 00:00:00');
// 当前时间
$currentTime = time();
// 计算网站运行时间(秒数)
$runningTime = $currentTime - $startTime;
// 转换为以年、天、小时、分钟和秒为单位的运行时间
$years = floor($runningTime / (365 * 24 * 60 * 60));
$runningTime %= (365 * 24 * 60 * 60);
$days = floor($runningTime / (24 * 60 * 60));
$runningTime %= (24 * 60 * 60);
$hours = floor($runningTime / (60 * 60));
$runningTime %= (60 * 60);
$minutes = floor($runningTime / 60);
$seconds = $runningTime % 60;
// 动态更新秒钟
echo '<script>setInterval(function() { document.getElementById('seconds').innerHTML = new Date().getSeconds(); }, 1000);</script>';
// 显示网站运行时间
echo '网站运行时间:';
echo "$years 年, $days 天, $hours 小时, $minutes 分钟, <span id='seconds'>$seconds</span> 秒";
?>
你可以将以上代码保存为一个 php 文件,并在网站页面中引用该文件,例如:
<!DOCTYPE html>
<html>
<head>
<title>网站运行时间</title>
</head>
<body>
<h1>网站运行时间</h1>
<?php include '运行时间.php'; ?>
</body>
</html>
这样,在浏览器中打开该页面时,将会显示网站的运行时间,秒钟会动态更新。
原文地址: https://www.cveoy.top/t/topic/QFH 著作权归作者所有。请勿转载和采集!