PHP计算人的当前年龄 [英] PHP calculate person's current age
问题描述
我的网站出生日期格式为 12.01.1980
。
$ person_date(string)= Day.Month.Year
想添加一个人。像目前30年(2010 - 1980年= 30年)。
但是,几年来,功能不能完美结果:
如果出生日期为 12.12.1980
,当前日期为 01.01。 2010
该人没有30岁。这是一个29年零一个月。
与目前的日期比较,必须计算出生年龄,月份和日期的 :
0)解析日期。
出生日期.Month.Year):
Day = $ birth_day;
Month = $ birth_month;
年= $ birth_year;
当前日期(Day.Month.Year):
Day = $ current_day;
Month = $ current_month;
年= $ current_year;
1)年比较,2010 - 1980 =写30(让它为变量)
2)比较月份,如果(出生日期月份大于当前月份)和01当前)){从 $ total_year
变量(30 - 1 = 29)}中减去一年。如果发生负值,则在此处完成计算。否则请去下一个(3步)。
3) else if(出生月份<当前月份){$ total_year = $ total_year ); } $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
并查看一天(在此步骤中):
if(出生日=当天){$ total_year = $ total_year; }
else if(出生日>当天){$ total_year = $ total_year -1; }
else if(出生日<当天){$ total_year = $ total_year; }
5)echo $ total_year;
我的php知识不好,希望你能帮忙。
谢谢。 b $ b
<?php
$ bday = new DateTime('12.12.1980');
// $ today = new DateTime('00:00:00'); - 用于当前日期
$ today = new DateTime('2010-08-01 00:00:00'); //用于测试目的
$ diff = $ today-> diff($ bday);
printf('%d年,%d月,%d天',$ diff-> y,$ diff-> m,$ diff-> d);
打印 29年,7个月,20天
I have birth dates on my site in format 12.01.1980
.
$person_date (string) = Day.Month.Year
Want to add an oldness of the person. Like "Currently 30 years" (2010 - 1980 = 30 years).
But makin the function just on years can't give the perfect result:
If person birth date is 12.12.1980
and current date is 01.01.2010
the person doesn't have 30 years old. It's a 29 years and one month.
There must be a calculation on targeting both year, month and day of birth with comparison of current date:
0) Parse the dates.
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) year comparison, 2010 - 1980 = write "30" (let it be $total_year
variable)
2) compare the months, if (birth date month is bigger > than current month (like 12 in birth and 01 current)) { do minus one year from $total_year
variable (30 - 1 = 29) }. If do minus happened, finish the calculations at this point. Else go the next (3 step).
3) else if (birth month < current month) { $total_year = $total_year (30); }
4) else if (birth month = current month) { $total_year = $total_year (30); }
and check the day (in this step):
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) echo $total_year;
My php knowledge isn't good, hope you can help.
Thanks.
You can use the DateTime class and its diff() method.
<?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);
prints 29 years, 7 month, 20 days
这篇关于PHP计算人的当前年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!