我在我的网站上的格式为12.01.1980
的出生日期。PHP计算人的当前年龄
$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)}。如果发生减号,则在此时完成计算。否则走下一步(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 2010-08-01 06:30:59
它会给出正确答案吗? – James 2010-08-01 06:34:33
@pascal:你如何计算日子? – Svish 2010-11-30 11:42:06