gpt4 book ai didi

php - 精确秒到 "day/month/year - minutes : hours : seconds"转换

转载 作者:行者123 更新时间:2023-12-05 08:14:42 27 4
gpt4 key购买 nike

我正在尝试将秒数转换为日期。这是我目前拥有的(在 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;
}

但这不够准确,因为它没有考虑到月份的不同长度......是否有已知的算法或其他东西可以正确地做到这一点??

提前致谢

编辑:

对不起,英语不是我的语言......我说我需要转换为日期,但我实际上做的是两个日期之间的差异,我在几秒钟内得到结果,我需要年、月、日、小时、分钟和秒的数量

最佳答案

我想这就是您要找的:

适用于 PHP >= 5.3.0

function secondsToTime($inputSeconds) {
$then = new DateTime(date('Y-m-d H:i:s', $inputSeconds));
$now = new DateTime(date('Y-m-d H:i:s', time()));
$diff = $then->diff($now);
return array('years' => $diff->y, 'months' => $diff->m, 'days' => $diff->d, 'hours' => $diff->h, 'minutes' => $diff->i, 'seconds' => $diff->s);
}

适用于 PHP >= 5.2.0

function secondsToTime($inputSeconds) {
$then = new DateTime(date('Y-m-d H:i:s', $inputSeconds));
$now = new DateTime(date('Y-m-d H:i:s', time()));
$years_then = $then->format('Y');
$years_now = $now->format('Y');
$years = $years_now - $years_then;

$months_then = $then->format('m');
$months_now = $now->format('m');
$months = $months_now - $months_then;

$days_then = $then->format('d');
$days_now = $now->format('d');
$days = $days_now - $days_then;

$hours_then = $then->format('H');
$hours_now = $now->format('H');
$hours = $hours_now - $hours_then;

$minutes_then = $then->format('i');
$minutes_now = $now->format('i');
$minutes = $minutes_now - $minutes_then;

$seconds_then = $then->format('s');
$seconds_now = $now->format('s');
$seconds = $seconds_now - $seconds_then;

if ($seconds < 0) {
$minutes -= 1;
$seconds += 60;
}
if ($minutes < 0) {
$hours -= 1;
$minutes += 60;
}
if ($hours < 0) {
$days -= 1;
$hours += 24;
}
$months_last = $months_now - 1;
if ($months_now == 1) {
$years_now -= 1;
$months_last = 12;
}
// Thank you, second grade. ;)
if ($months_last == 9 || $months_last == 4 || $months_last == 6 || $months_last == 11) {
$days_last_month = 30;
}
else if ($months_last == 2) {
if (($years_now % 4) == 0) {
$days_last_month = 29;
}
else {
$days_last_month = 28;
}
}
else {
$days_last_month = 31;
}
if ($days < 0) {
$months -= 1;
$days += $days_last_month;
}
if ($months < 0) {
$years -= 1;
$months += 12;
}
return array('years' => $years, 'months' => $months, 'days' => $days, 'hours' => $hours, 'minutes' => $minutes, 'seconds' => $seconds);
}

关于php - 精确秒到 "day/month/year - minutes : hours : seconds"转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19728913/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com