将秒转换为天、小时、分钟和秒我想转换一个变量$uptime这是秒,分为几天、几个小时、几分钟和几秒。例子:$uptime = 1640467;结果应该是:18 days 23 hours 41 minutes
3 回答
杨魅力
TA贡献1811条经验 获得超6个赞
DateTime
用途:
echo secondsToTime(1640467);# 18 days, 23 hours, 41 minutes and 7 seconds
职能:
function secondsToTime($seconds) { $dtF = new \DateTime('@0'); $dtT = new \DateTime("@$seconds"); return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');}
演示
慕少森
TA贡献2019条经验 获得超9个赞
这与最高投票的答案之间的区别是:
259264
3天1分4秒
259264
三天, 0小时,1分钟 S和4秒
function secondsToTime($inputSeconds) { $secondsInAMinute = 60; $secondsInAnHour = 60 * $secondsInAMinute; $secondsInADay = 24 * $secondsInAnHour; // Extract days $days = floor($inputSeconds / $secondsInADay); // Extract hours $hourSeconds = $inputSeconds % $secondsInADay; $hours = floor($hourSeconds / $secondsInAnHour); // Extract minutes $minuteSeconds = $hourSeconds % $secondsInAnHour; $minutes = floor($minuteSeconds / $secondsInAMinute); // Extract the remaining seconds $remainingSeconds = $minuteSeconds % $secondsInAMinute; $seconds = ceil($remainingSeconds); // Format and return $timeParts = []; $sections = [ 'day' => (int)$days, 'hour' => (int)$hours, 'minute' => (int)$minutes, 'second' => (int)$seconds, ]; foreach ($sections as $name => $value){ if ($value > 0){ $timeParts[] = $value. ' '.$name.($value == 1 ? '' : 's'); } } return implode(', ', $timeParts);}
- 3 回答
- 0 关注
- 419 浏览
添加回答
举报
0/150
提交
取消