以下是一个用 PHP 编写的代码示例,用于计算网站运行时间,并以年、天、小时、分钟和秒为单位显示:

<?php

// 设置网站创建的时间
$websiteCreationTime = strtotime('2020-01-01 00:00:00');

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

// 计算网站运行时间(秒)
$websiteRunningTime = $currentTime - $websiteCreationTime;

// 计算年数
$years = floor($websiteRunningTime / (365 * 24 * 60 * 60));

// 计算天数
$days = floor(($websiteRunningTime % (365 * 24 * 60 * 60)) / (24 * 60 * 60));

// 计算小时数
$hours = floor(($websiteRunningTime % (24 * 60 * 60)) / (60 * 60));

// 计算分钟数
$minutes = floor(($websiteRunningTime % (60 * 60)) / 60);

// 计算秒数
$seconds = $websiteRunningTime % 60;

// 输出网站运行时间
echo '网站运行时间:';
echo $years . '年, '; 
echo $days . '天, '; 
echo $hours . '小时, '; 
echo $minutes . '分钟, '; 
echo $seconds . '秒';

?>

请注意,这只是一个简单的示例代码,它假设网站创建时间为 2020-01-01 00:00:00。你可以根据实际情况调整 $websiteCreationTime 的值。


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

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