我在我的網站上的格式爲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