【问题标题】:Algorithm for getting the Age and the leap years since birth获取出生后年龄和闰年的算法
【发布时间】:2014-04-30 05:26:02
【问题描述】:

计算自他或她出生年份以来的闰年总数。

来自用户的示例输入:1993 并且输出应该是:2562

更新:

到目前为止,这是我的代码,如果特定年份是闰年,我创建了一个算法。

if($yr%4 == 0) {

 $leaps++;

}

考虑从 1993 年开始制作一个循环,并每年检查是否是闰年。

update2已解决。这就是我所做的。谢谢大家

<?php 

    $dt = date("Y");

    $nm = $_POST['name'];
    $yr = $_POST['year'];
    $leaps = 0;
    $total = 0;
    $age = $dt - $yr;

    for($i = $yr; $i < $dt ; $i++) {

        if($i %4 == 0) {

            $leaps++;

        }

        $total = 366 * $leaps;
    }

    echo "Hi $nm, you are $age years old. There were $total since your birthday";   

?>

【问题讨论】:

  • 到目前为止您尝试过什么?请发布您的代码。
  • 你的算法在公历计算闰年时是错误的。见算法here >>> if year is not divisible by 4 then common year / else if year is not divisible by 100 then leap year / else if year is divisible by 400 then leap year / else common year

标签: php date date-difference


【解决方案1】:

这里就是你要找的东西:

<?php

     $now = time(); // or your date as well
     $your_date = strtotime("1993-01-01");
     $datediff = $now - $your_date;
     echo floor($datediff/(60*60*24));

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    相关资源
    最近更新 更多