PHP 网站运行时间计算:从年到秒的动态显示
以下是使用 PHP 编写的一个简单的网站运行时间代码,它将以年、天、小时、分钟和秒的形式显示网站已运行的时间,其中秒钟将是动态的:
<?php
// 网站启动时间(替换为您的实际启动时间)
$websiteLaunchTime = strtotime('2022-01-01 00:00:00');
// 当前时间
$currentTime = time();
// 计算网站运行时间(秒)
$runningTime = $currentTime - $websiteLaunchTime;
// 将运行时间转换为年、天、小时、分钟和秒
$years = floor($runningTime / (365 * 24 * 60 * 60));
$days = floor(($runningTime % (365 * 24 * 60 * 60)) / (24 * 60 * 60));
$hours = floor(($runningTime % (24 * 60 * 60)) / (60 * 60));
$minutes = floor(($runningTime % (60 * 60)) / 60);
$seconds = $runningTime % 60;
// 显示网站运行时间
echo '网站已运行时间:';
echo $years . '年, ';
echo $days . '天, ';
echo $hours . '小时, ';
echo $minutes . '分钟, ';
echo $seconds . '秒钟';
?>
这段代码首先定义了网站的启动时间 $websiteLaunchTime,请根据实际情况将其替换为网站的实际启动时间。然后,它获取当前时间 $currentTime,计算网站的运行时间 $runningTime(以秒为单位)。接下来,通过对运行时间进行适当的计算,将其转换为年、天、小时、分钟和秒。最后,使用 echo 语句将网站的运行时间以友好的格式输出。
原文地址: https://www.cveoy.top/t/topic/C6k 著作权归作者所有。请勿转载和采集!