将秒转换为天、小时、分钟和秒
我想将秒的变量转换为天,小时,分钟和秒。$uptime
例:
$uptime = 1640467;
结果应该是:
18 days 23 hours 41 minutes
我想将秒的变量转换为天,小时,分钟和秒。$uptime
例:
$uptime = 1640467;
结果应该是:
18 days 23 hours 41 minutes
这是重写的函数,以包括天数。我还更改了变量名称,以使代码更易于理解...
/**
* Convert number of seconds into hours, minutes and seconds
* and return an array containing those values
*
* @param integer $inputSeconds Number of seconds to parse
* @return array
*/
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);
// return the final array
$obj = array(
'd' => (int) $days,
'h' => (int) $hours,
'm' => (int) $minutes,
's' => (int) $seconds,
);
return $obj;
}
来源: CodeAid() - http://codeaid.net/php/convert 秒到小时分秒(php)