我正在尝试将秒数转换为日期。这是我目前拥有的(在 php 中):
function secondsToTime($inputSeconds) {
$secondsInAMinute = 60;
$secondsInAnHour = 60 * $secondsInAMinute;
$secondsInADay = 24 * $secondsInAnHour;
$secondsInAMonth = 30 * $secondsInADay;
$secondsInAYear = 12 * $secondsInAMonth;
$years = floor($inputSeconds / $secondsInAYear);
$monthSeconds = $inputSeconds % $secondsInAYear;
$months = floor($monthSeconds / $secondsInAMonth);
$daySeconds = $monthSeconds % $secondsInAMonth;
$days = floor($daySeconds / $secondsInADay);
$hourSeconds = $daySeconds % $secondsInADay;
$hours = floor($hourSeconds / $secondsInAnHour);
$minuteSeconds = $hourSeconds % $secondsInAnHour;
$minutes = floor($minuteSeconds / $secondsInAMinute);
$remainingSeconds = $minuteSeconds % $secondsInAMinute;
$seconds = ceil($remainingSeconds);
$obj = array(
'years' => (int) $years,
'months' => (int) $months,
'days' => (int) $days,
'hours' => (int) $hours,
'minutes' => (int) $minutes,
'seconds' => (int) $seconds
);
return $obj;
}
但这还不够准确,因为它没有考虑到月份的不同长度……是否有已知的算法或其他东西可以正确执行?
提前致谢
编辑:
对不起,英语不是我的语言......我说我需要转换为日期,但我实际上在做的是两个日期之间的差异,我在几秒钟内得到结果,我需要年、月、日、小时、分钟和秒