以下是使用 PHP 编写一个显示网站运行时间的代码。该代码会将网站的运行时间从年到秒以动态方式显示出来。

<?php
// 设置网站的启动时间
$startTime = strtotime('2022-01-01 00:00:00');

// 计算当前时间与启动时间之间的时间差
$currentTime = time();
$timeDifference = $currentTime - $startTime;

// 计算年、月、日、小时、分钟和秒钟
$years = floor($timeDifference / (365 * 24 * 60 * 60));
$months = floor(($timeDifference - $years * 365 * 24 * 60 * 60) / (30 * 24 * 60 * 60));
$days = floor(($timeDifference - $years * 365 * 24 * 60 * 60 - $months * 30 * 24 * 60 * 60) / (24 * 60 * 60));
$hours = floor(($timeDifference - $years * 365 * 24 * 60 * 60 - $months * 30 * 24 * 60 * 60 - $days * 24 * 60 * 60) / (60 * 60));
$minutes = floor(($timeDifference - $years * 365 * 24 * 60 * 60 - $months * 30 * 24 * 60 * 60 - $days * 24 * 60 * 60 - $hours * 60 * 60) / 60);
$seconds = $timeDifference - $years * 365 * 24 * 60 * 60 - $months * 30 * 24 * 60 * 60 - $days * 24 * 60 * 60 - $hours * 60 * 60 - $minutes * 60;

// 显示网站运行时间
echo '网站运行时间:';
echo $years . '年, '; 
echo $months . '月, '; 
echo $days . '天, '; 
echo $hours . '小时, '; 
echo $minutes . '分钟, '; 
echo $seconds . '秒';
?>

这个代码会根据启动时间计算当前时间与启动时间之间的时间差,并将时间差转换为年、月、日、小时、分钟和秒钟。最后,它会以动态方式显示出网站的运行时间。


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

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