Perhaps I'll blog in detail more about these functions later, but for now, I'm going to present to you 3 of the more popular age formulas and the results I've found.
ALERT: function getAge_2 seems to be the most popular one I've found on the net and is the one that tends to be wrong!
First, the functions
function getAge_1($date) { // Y-m-d format
$now = explode("-", date('Y-m-d'));
$dob = explode("-", $date);
$dif = $now[0] - $dob[0];
if ($dob[1] > $now[1]) { // birthday month has not hit this year
$dif -= 1;
}
elseif ($dob[1] == $now[1]) { // birthday month is this month, check day
if ($dob[2] > $now[2]) {
$dif -= 1;
}
elseif ($dob[2] == $now[2]) { // Happy Birthday!
//$dif = $dif." Happy Birthday!";
};
};
return $dif;
}
function getAge_2($date) { // Y-m-d format
return floor((time() - strtotime($date))/(60*60*24*365.2425));
}
function getAge_3($date) { // Y-m-d format
return intval(substr(date('Ymd') - date('Ymd', strtotime($date)), 0, -4));
}
The following results span 100 years from 1/1/1902 to today, 3/29/2012. As you will see in the following
two table
s, the key difference was found in getting the age of someone with a birthday tomorrow, expect on some years before 1932 where the difference was also found for the day AFTER tomorrow.
The Difference Table
|
Year
|
getAge_1
|
getAge_2
|
getAge_3
|
*1902-03-30* | 109 | 110 | 109 | *1903-03-30* | 108 | 109 | 108 | *1903-03-31* | 108 | 109 | 108 | *1904-03-30* | 107 | 108 | 107 | *1905-03-30* | 106 | 107 | 106 | *1906-03-30* | 105 | 106 | 105 | *1907-03-30* | 104 | 105 | 104 | *1907-03-31* | 104 | 105 | 104 | *1908-03-30* | 103 | 104 | 103 | *1909-03-30* | 102 | 103 | 102 | *1910-03-30* | 101 | 102 | 101 | *1911-03-30* | 100 | 101 | 100 | *1911-03-31* | 100 | 101 | 100 | *1912-03-30* | 99 | 100 | 99 | *1913-03-30* | 98 | 99 | 98 | *1914-03-30* | 97 | 98 | 97 | *1915-03-30* | 96 | 97 | 96 | *1915-03-31* | 96 | 97 | 96 | *1916-03-30* | 95 | 96 | 95 | *1917-03-30* | 94 | 95 | 94 | *1918-03-30* | 93 | 94 | 93 | *1919-03-30* | 92 | 93 | 92 | *1919-03-31* | 92 | 93 | 92 | *1920-03-30* | 91 | 92 | 91 | *1921-03-30* | 90 | 91 | 90 | *1922-03-30* | 89 | 90 | 89 | *1923-03-30* | 88 | 89 | 88 | *1923-03-31* | 88 | 89 | 88 | *1924-03-30* | 87 | 88 | 87 | *1925-03-30* | 86 | 87 | 86 | *1926-03-30* | 85 | 86 | 85 | *1927-03-30* | 84 | 85 | 84 | *1927-03-31* | 84 | 85 | 84 | *1928-03-30* | 83 | 84 | 83 | *1929-03-30* | 82 | 83 | 82 | *1930-03-30* | 81 | 82 | 81 | *1931-03-30* | 80 | 81 | 80 | *1931-03-31* | 80 | 81 | 80 | *1932-03-30* | 79 | 80 | 79 | *1933-03-30* | 78 | 79 | 78 | *1934-03-30* | 77 | 78 | 77 | *1935-03-30* | 76 | 77 | 76 | *1935-03-31* | 76 | 77 | 76 | *1936-03-30* | 75 | 76 | 75 | *1937-03-30* | 74 | 75 | 74 | *1938-03-30* | 73 | 74 | 73 | *1939-03-30* | 72 | 73 | 72 | *1940-03-30* | 71 | 72 | 71 | *1941-03-30* | 70 | 71 | 70 | *1942-03-30* | 69 | 70 | 69 | *1943-03-30* | 68 | 69 | 68 | *1944-03-30* | 67 | 68 | 67 | *1945-03-30* | 66 | 67 | 66 | *1946-03-30* | 65 | 66 | 65 | *1947-03-30* | 64 | 65 | 64 | *1948-03-30* | 63 | 64 | 63 | *1949-03-30* | 62 | 63 | 62 | *1950-03-30* | 61 | 62 | 61 | *1951-03-30* | 60 | 61 | 60 | *1952-03-30* | 59 | 60 | 59 | *1953-03-30* | 58 | 59 | 58 | *1954-03-30* | 57 | 58 | 57 | *1955-03-30* | 56 | 57 | 56 | *1956-03-30* | 55 | 56 | 55 | *1957-03-30* | 54 | 55 | 54 | *1958-03-30* | 53 | 54 | 53 | *1959-03-30* | 52 | 53 | 52 | *1960-03-30* | 51 | 52 | 51 | *1961-03-30* | 50 | 51 | 50 | *1962-03-30* | 49 | 50 | 49 | *1963-03-30* | 48 | 49 | 48 | *1964-03-30* | 47 | 48 | 47 | *1965-03-30* | 46 | 47 | 46 | *1966-03-30* | 45 | 46 | 45 | *1967-03-30* | 44 | 45 | 44 | *1968-03-30* | 43 | 44 | 43 | *1969-03-30* | 42 | 43 | 42 | *1970-03-30* | 41 | 42 | 41 | *1971-03-30* | 40 | 41 | 40 | *1973-03-30* | 38 | 39 | 38 | *1974-03-30* | 37 | 38 | 37 | *1975-03-30* | 36 | 37 | 36 | *1977-03-30* | 34 | 35 | 34 | *1978-03-30* | 33 | 34 | 33 | *1979-03-30* | 32 | 33 | 32 | *1981-03-30* | 30 | 31 | 30 | *1982-03-30* | 29 | 30 | 29 | *1983-03-30* | 28 | 29 | 28 | *1985-03-30* | 26 | 27 | 26 | *1986-03-30* | 25 | 26 | 25 | *1987-03-30* | 24 | 25 | 24 | *1989-03-30* | 22 | 23 | 22 | *1990-03-30* | 21 | 22 | 21 | *1991-03-30* | 20 | 21 | 20 | *1993-03-30* | 18 | 19 | 18 | *1994-03-30* | 17 | 18 | 17 | *1995-03-30* | 16 | 17 | 16 | *1997-03-30* | 14 | 15 | 14 | *1998-03-30* | 13 | 14 | 13 | *1999-03-30* | 12 | 13 | 12 | *2001-03-30* | 10 | 11 | 10 | *2002-03-30* | 9 | 10 | 9 | *2003-03-30* | 8 | 9 | 8 | *2005-03-30* | 6 | 7 | 6 | *2006-03-30* | 5 | 6 | 5 | *2007-03-30* | 4 | 5 | 4 | *2010-03-30* | 1 | 2 | 1 | *2011-03-30* | 0 | 1 | 0 |
|
The Full table was way to big, so i'll find another way to show it later, gota get back to the grind, this was just to help others know! Laters!
JD,
ReplyDeleteDon't know if you can help me or not but I'm trying to calculate the number of days old an invoice is from a mysql dbase with a data field of invdte (invoice date) and data field aging. I need the aging field to be calculated from whatever the current date is lookup up the invdte and auto increment every day until it is removed when PAID. I hope I explained this correctly.
Deoesn't sound hard, I will do what I can to help ya, email me at spyk3hh@gmail.com. Just from first read, I assume you're using PHP back-end with mysql and you want to get a previously saved date from my sql, compare it to today, then save the difference in another mysql field?
DeleteI have slightly different php code that appears to work fine and seems similar to your getAge_1 but just curious to see if you notice any problems or have a comment? your post helped a lot! thanks!
ReplyDeletefunction birthday($birthday){
list($year,$month,$day) = explode("-",$birthday);
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
if ($month_diff <= 0)
if ($day_diff < 0)
$year_diff--;
return $year_diff;
}
Very helpful post. Thanks
ReplyDelete