gpt4 book ai didi

php - 将天数转换为年数和剩余天数

转载 作者:行者123 更新时间:2023-12-02 06:33:49 24 4
gpt4 key购买 nike

我正在尝试将天数转换为年数和剩余天数格式。这是我尝试过的:

<?php
$reg_date = date('Y-m-d', strtotime($whois_details[5])); // 1997-09-15
$total_days = (date('Y-m-d') - $reg_date) * 365; // 2014-07-30
$total_years = intval($total_days / 365);
$remaining_days = ($total_days % 365) % 30;
if ($total_days < 365) {
$remaining_days = $total_days;
}
echo $total_years.' years and '.$remaining_days.' days';
?>

输出 - 300

0 years and 300 days - THIS IS OK

输出 - 500

1 years and 15 days - THIS IS NOT OK

http://codepad.org/MDQjsz5l

应该是 - 500

1 years and 135 days

我错了什么。我检查了 C 尖锐的问题并尝试转换它。

最佳答案

我不知道你复制的代码为什么会这样,你能链接一下吗?

这当然忽略了日期的复杂性(如闰年),但作为此算法通常如何工作的示例。要考虑闰年,您需要知道所讨论的时间跨度是否包括闰年。如果给你的只是几天的时间,这大概是你能做的最好的事情了。

$days = 500;
$years_remaining = intval($days / 365); //divide by 365 and throw away the remainder
$days_remaining = $days % 365; //divide by 365 and *return* the remainder

关于php - 将天数转换为年数和剩余天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25038536/

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