从我收集的信息来看,您似乎有兴趣返回一个字符串,其值包含用冒号分隔的两位数字。假设定位不需要改变,你可以做类似下面的事情:
<?php
// Prefix single-digit values with a zero.
function ensure2Digit($number) {
if($number < 10) {
$number = '0' . $number;
}
return $number;
}
// Convert seconds into months, days, hours, minutes, and seconds.
function secondsToTime($ss) {
$s = ensure2Digit($ss%60);
$m = ensure2Digit(floor(($ss%3600)/60));
$h = ensure2Digit(floor(($ss%86400)/3600));
$d = ensure2Digit(floor(($ss%2592000)/86400));
$M = ensure2Digit(floor($ss/2592000));
return "$M:$d:$h:$m:$s";
}
或者,如果您不喜欢管理更多功能的想法,也许这可能更适合您:
<?php
// Convert seconds into months, days, hours, minutes, and seconds.
function secondsToTime($ss) {
$s = $ss%60;
$m = floor(($ss%3600)/60);
$h = floor(($ss%86400)/3600);
$d = floor(($ss%2592000)/86400);
$M = floor($ss/2592000);
// Ensure all values are 2 digits, prepending zero if necessary.
$s = $s < 10 ? '0' . $s : $s;
$m = $m < 10 ? '0' . $m : $m;
$h = $h < 10 ? '0' . $h : $h;
$d = $d < 10 ? '0' . $d : $d;
$M = $M < 10 ? '0' . $M : $M;
return "$M:$d:$h:$m:$s";
}
然后调用我们的函数(无论您决定使用哪种方法):
<?php
$ss = 123456;
print secondsToTime($ss);
?>