PHP 网站运行时间代码:年、月、日、时、分、秒动态显示
以下是使用 PHP 编写的一个简单的网站运行时间代码,它将显示从网站启动到当前时间的运行时间,并以年、月、日、小时、分钟和秒的形式显示。代码中使用了动态的秒钟部分。
<?php
// 网站启动时间(自行设置)
$startTime = strtotime('2022-01-01 00:00:00');
// 当前时间
$currentTime = time();
// 计算运行时间(以秒为单位)
$runTime = $currentTime - $startTime;
// 计算年、月、日、小时、分钟和秒
$years = floor($runTime / (365 * 24 * 60 * 60));
$months = floor(($runTime - ($years * 365 * 24 * 60 * 60)) / (30 * 24 * 60 * 60));
$days = floor(($runTime - ($years * 365 * 24 * 60 * 60) - ($months * 30 * 24 * 60 * 60)) / (24 * 60 * 60));
$hours = floor(($runTime - ($years * 365 * 24 * 60 * 60) - ($months * 30 * 24 * 60 * 60) - ($days * 24 * 60 * 60)) / (60 * 60));
$minutes = floor(($runTime - ($years * 365 * 24 * 60 * 60) - ($months * 30 * 24 * 60 * 60) - ($days * 24 * 60 * 60) - ($hours * 60 * 60)) / 60);
$seconds = $runTime % 60;
// 显示网站运行时间
echo '网站运行时间:' . $years . '年, ' . $months . '月, ' . $days . '天, ' . $hours . '小时, ' . $minutes . '分钟, ' . $seconds . '秒';
?>
这段代码使用strtotime函数来设置网站的启动时间,并使用time函数获取当前时间。然后,计算两者之间的差值,得到网站的运行时间。通过对差值进行适当的计算,可以得到年、月、日、小时、分钟和秒钟的动态值。最后,将这些值格式化并输出到网页上。
原文地址: https://www.cveoy.top/t/topic/mRo 著作权归作者所有。请勿转载和采集!