php 用PHP从出生日期计算年龄

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10186811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:36:08  来源:igfitidea点击:

Calculating age from date of birth in PHP

php

提问by Mandeep Singh

What's the most precise function you have come across to work out an age from the users date of birth. I have the following code and was wondering how it could be improved as it doesn't support all date formats and not sure if it's the most accurate function either (DateTime compliance would be nice).

从用户的出生日期算出年龄的最精确函数是什么?我有以下代码,想知道如何改进它,因为它不支持所有日期格式,并且不确定它是否是最准确的功能(DateTime 合规性会很好)。

function getAge($birthday) {
    return floor((strtotime(date('d-m-Y')) - strtotime($date))/(60*60*24*365.2421896));
}

回答by KingCrunch

$birthday = new DateTime($birthday);
$interval = $birthday->diff(new DateTime);
echo $interval->y;

Should work

应该管用

回答by Dinithi

Check this

检查这个

<?php
$c= date('Y');
$y= date('Y',strtotime('1988-12-29'));
echo $c-$y;
?>

回答by Mandeep Singh

Use this code to have full age including years, months and days-

使用此代码获得完整年龄,包括年、月和日-

    <?php
     //full age calulator
     $bday = new DateTime('02.08.1991');//dd.mm.yyyy
     $today = new DateTime('00:00:00'); // Current date
     $diff = $today->diff($bday);
     printf('%d years, %d month, %d days', $diff->y, $diff->m, $diff->d);
    ?>

回答by John Conde

Try using DateTimefor this:

尝试为此使用DateTime

$now      = new DateTime();
$birthday = new DateTime('1973-04-18 09:48:00');
$interval = $now->diff($birthday);
echo $interval->format('%y years'); // 39 years

See it in action

看到它在行动

回答by Luke Watts

For supper accuracy you need to account for the leap year factor:

为了获得更高的精度,您需要考虑闰年因素:

function get_age($dob_day,$dob_month,$dob_year){
    $year   = gmdate('Y');
    $month  = gmdate('m');
    $day    = gmdate('d');
     //seconds in a day = 86400
    $days_in_between = (mktime(0,0,0,$month,$day,$year) - mktime(0,0,0,$dob_month,$dob_day,$dob_year))/86400;
    $age_float = $days_in_between / 365.242199; // Account for leap year
    $age = (int)($age_float); // Remove decimal places without rounding up once number is + .5
    return $age;
}

So use:

所以使用:

echo get_date(31,01,1985);

or whatever...

管他呢...

N.B. To see your EXACT age to the decimal

注意要查看您的精确年龄到小数点

return $age_float

instead.

反而。

回答by Subin

This function works fine.

此功能工作正常。

function age($birthday){
 list($day,$month,$year) = explode("/",$birthday);
 $year_diff  = date("Y") - $year;
 $month_diff = date("m") - $month;
 $day_diff   = date("d") - $day;
 if ($day_diff < 0 && $month_diff==0){$year_diff--;}
 if ($day_diff < 0 && $month_diff < 0){$year_diff--;}
 return $year_diff;
}

See BLOG Post

见博客帖子

回答by Milan Halada

This works:

这有效:

<?
$date = date_create('1984-10-26');
$interval = $date->diff(new DateTime);
echo $interval->y;
?>

If you tell me in what format your $birthdayvariable comes I will give you exact solution

如果你告诉我你的$birthday变量以什么格式出现,我会给你确切的解决方案

回答by New in php

Change the $dateto $birthday.

将 更改$date$birthday

回答by symcbean

WTF?

跆拳道?

strtotime(date('d-m-Y'))

strtotime(日期('dm-Y'))

So you generate a date string from the current timestamp, then convert the date string back into a timestamp?

所以你从当前时间戳生成一个日期字符串,然后将日期字符串转换回时间戳?

BTW, one of the reasons it's not working is that strtotime() assumes numeric dates to be in the format m/d/y (i.e. the US format of date first). Another reason is that the parameter ($birthday) is not used in the formula.

顺便说一句,它不起作用的原因之一是 strtotime() 假定数字日期的格式为 m/d/y (即美国日期格式优先)。另一个原因是公式中没有使用参数 ($birthday)。

回答by jb_

Here is my long/detailed version (you can make it shorter if you want):

这是我的长/详细版本(如果您愿意,可以缩短它):

$timestamp_birthdate = mktime(9, 0, 0, $birthdate_month, $birthdate_day, $birthdate_year);
$timestamp_now = time();
$difference_seconds = $timestamp_now-$timestamp_birthdate;
$difference_minutes = $difference_seconds/60;
$difference_hours = $difference_minutes/60;
$difference_days = $difference_hours/24;
$difference_years = $difference_days/365;