【问题标题】:PHP calculate person's current agePHP计算人的当前年龄
【发布时间】:2011-03-23 19:10:57
【问题描述】:

我的网站上有12.01.1980 格式的出生日期。

$person_date (string) = Day.Month.Year

想添加一个人的故乡。比如“目前 30 年”(2010 - 1980 = 30 年)。

但是仅仅在几年内完成这个功能并不能给出完美的结果:

如果人的出生日期是 12.12.1980 并且当前日期是 01.01.2010,则此人未满 30 岁。 29年零一个月。

必须通过比较当前日期来计算定位出生年月日

0) 解析日期。

Birth date (Day.Month.Year):
Day = $birth_day;
Month = $birth_month;
Year = $birth_year;

Current date (Day.Month.Year):
Day = $current_day;
Month = $current_month;
Year = $current_year;

1) 年份比较,2010 - 1980 = 写“30”(让它成为$total_year 变量)

2) 比较月份,如果(出生日期月份大于当前月份(例如出生日期为 12,当前月份为 01)){从$total_year 变量 (30 - 1 = 29) 中减去一年}。如果do减发生,此时完成计算。否则进行下一步(3 步)。

3)else if (birth month < current month) { $total_year = $total_year (30); }

4) else if (birth month = current month) { $total_year = $total_year (30); }

并检查日期(在此步骤中):

 if(birth day = current day) { $total_year = $total_year; }
 else if (birth day > current day) { $total_year = $total_year -1; }
 else if (birth day < current day) { $total_year = $total_year; }

5) 回显 $total_year;

我的php知识不太好,希望大家帮忙

谢谢。

【问题讨论】:

  • 计算出生日期和现在之间的天数,乘以 4,除以 1461(而不是浮点除以 365.25)?
  • @pascal:你怎么算日子?

标签: php date


【解决方案1】:

您可以使用DateTime class 及其diff() 方法。

<?php
$bday = new DateTime('12.12.1980');
// $today = new DateTime('00:00:00'); - use this for the current date
$today = new DateTime('2010-08-01 00:00:00'); // for testing purposes

$diff = $today->diff($bday);

printf('%d years, %d month, %d days', $diff->y, $diff->m, $diff->d);

打印29 years, 7 month, 20 days

【讨论】:

    【解决方案2】:

    @VolkerK 答案的扩展 - 太棒了!我从不喜欢看到零岁,如果你只使用年份就会发生这种情况。此功能以月为单位显示他们的年龄(如果他们是一个月大或更多),否则以天为单位。

    function calculate_age($birthday)
    {
        $today = new DateTime();
        $diff = $today->diff(new DateTime($birthday));
    
        if ($diff->y)
        {
            return $diff->y . ' years';
        }
        elseif ($diff->m)
        {
            return $diff->m . ' months';
        }
        else
        {
            return $diff->d . ' days';
        }
    }
    

    【讨论】:

    • 这里做得很好@jonathan。它是 VolkerK 工作的一个真正常识性的延伸。我再次修改它以提供更“人性化”的读数,见下文。谢谢!
    【解决方案3】:

    我进一步扩展了@Jonathan 的回答,以提供更“人性化”的回答。

    使用这些日期:

    $birthday= new DateTime('2011-11-21');
    //Your date of birth.
    

    并调用此函数:

    function calculate_age($birthday)
    {
        $today = new DateTime();
        $diff = $today->diff(new DateTime($birthday));
    
        if ($diff->y)
        {
            return 'Age: ' . $diff->y . ' years, ' . $diff->m . ' months';
        }
        elseif ($diff->m)
        {
            return 'Age: ' . $diff->m . ' months, ' . $diff->d . ' days';
        }
        else
        {
            return 'Age: ' . $diff->d . ' days old!';
        }
    }; 
    

    正在返回:

    Age: 1 years, 2 months
    

    可爱 - 对于只有几天大的真正年轻的人!

    【讨论】:

    • $birthday 应该是 $bday
    猜你喜欢
    • 1970-01-01
    • 2017-11-21
    • 2013-06-29
    • 2020-12-28
    • 2011-04-16
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多