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

<?php
// 获取网站开始运行的时间戳
$startTime = strtotime('2022-01-01 00:00:00'); // 假设网站开始运行的时间为2022年1月1日

// 获取当前时间戳
$currentTime = time();

// 计算网站运行的总秒数
$seconds = $currentTime - $startTime;

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

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

这段代码会根据当前时间和网站开始运行的时间计算出网站运行的年、天、小时、分钟和秒,并将其输出。请注意,这个代码是基于当前时间计算运行时间,因此在每次刷新页面时会动态显示秒的变化。


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

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