以下是一个使用 PHP 编写的网站运行时间的代码,单位从年到秒,并且秒钟是动态的:

<?php
// 获取当前时间戳
$currentTimestamp = time();

// 定义网站启动时间戳(此处为示例,可以根据实际情况修改)
$websiteLaunchTimestamp = strtotime('2022-01-01 00:00:00');

// 计算网站运行时间(单位:秒)
$runningTimeSeconds = $currentTimestamp - $websiteLaunchTimestamp;

// 将秒数转换为年、天、小时、分钟和秒
$years = floor($runningTimeSeconds / (365 * 24 * 60 * 60));
$days = floor(($runningTimeSeconds - ($years * 365 * 24 * 60 * 60)) / (24 * 60 * 60));
$hours = floor(($runningTimeSeconds - ($years * 365 * 24 * 60 * 60) - ($days * 24 * 60 * 60)) / (60 * 60));
$minutes = floor(($runningTimeSeconds - ($years * 365 * 24 * 60 * 60) - ($days * 24 * 60 * 60) - ($hours * 60 * 60)) / 60);
$seconds = $runningTimeSeconds % 60;

// 输出网站运行时间
echo '网站已运行:';
if ($years > 0) {
    echo $years . '年, ';  
}
if ($days > 0) {
    echo $days . '天, ';  
}
if ($hours > 0) {
    echo $hours . '小时, ';  
}
if ($minutes > 0) {
    echo $minutes . '分钟, ';  
}
echo $seconds . '秒';
?>

这段代码首先获取当前时间戳和网站启动时间戳,然后计算二者之间的差值,即网站的运行时间。接下来,将总秒数转换为年、天、小时、分钟和秒,并输出到浏览器中。如果某个时间单位为0,则不会显示在输出中。

请注意,这里的网站启动时间戳是一个示例,你需要根据实际情况将其修改为你网站的实际启动时间。


原文地址: http://www.cveoy.top/t/topic/HtM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录