Thursday, March 29, 2012

PHP: Get Age Comparison Results of 3 Functions

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 tables, 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*109110109
*1903-03-30*108109108
*1903-03-31*108109108
*1904-03-30*107108107
*1905-03-30*106107106
*1906-03-30*105106105
*1907-03-30*104105104
*1907-03-31*104105104
*1908-03-30*103104103
*1909-03-30*102103102
*1910-03-30*101102101
*1911-03-30*100101100
*1911-03-31*100101100
*1912-03-30*9910099
*1913-03-30*989998
*1914-03-30*979897
*1915-03-30*969796
*1915-03-31*969796
*1916-03-30*959695
*1917-03-30*949594
*1918-03-30*939493
*1919-03-30*929392
*1919-03-31*929392
*1920-03-30*919291
*1921-03-30*909190
*1922-03-30*899089
*1923-03-30*888988
*1923-03-31*888988
*1924-03-30*878887
*1925-03-30*868786
*1926-03-30*858685
*1927-03-30*848584
*1927-03-31*848584
*1928-03-30*838483
*1929-03-30*828382
*1930-03-30*818281
*1931-03-30*808180
*1931-03-31*808180
*1932-03-30*798079
*1933-03-30*787978
*1934-03-30*777877
*1935-03-30*767776
*1935-03-31*767776
*1936-03-30*757675
*1937-03-30*747574
*1938-03-30*737473
*1939-03-30*727372
*1940-03-30*717271
*1941-03-30*707170
*1942-03-30*697069
*1943-03-30*686968
*1944-03-30*676867
*1945-03-30*666766
*1946-03-30*656665
*1947-03-30*646564
*1948-03-30*636463
*1949-03-30*626362
*1950-03-30*616261
*1951-03-30*606160
*1952-03-30*596059
*1953-03-30*585958
*1954-03-30*575857
*1955-03-30*565756
*1956-03-30*555655
*1957-03-30*545554
*1958-03-30*535453
*1959-03-30*525352
*1960-03-30*515251
*1961-03-30*505150
*1962-03-30*495049
*1963-03-30*484948
*1964-03-30*474847
*1965-03-30*464746
*1966-03-30*454645
*1967-03-30*444544
*1968-03-30*434443
*1969-03-30*424342
*1970-03-30*414241
*1971-03-30*404140
*1973-03-30*383938
*1974-03-30*373837
*1975-03-30*363736
*1977-03-30*343534
*1978-03-30*333433
*1979-03-30*323332
*1981-03-30*303130
*1982-03-30*293029
*1983-03-30*282928
*1985-03-30*262726
*1986-03-30*252625
*1987-03-30*242524
*1989-03-30*222322
*1990-03-30*212221
*1991-03-30*202120
*1993-03-30*181918
*1994-03-30*171817
*1995-03-30*161716
*1997-03-30*141514
*1998-03-30*131413
*1999-03-30*121312
*2001-03-30*101110
*2002-03-30*9109
*2003-03-30*898
*2005-03-30*676
*2006-03-30*565
*2007-03-30*454
*2010-03-30*121
*2011-03-30*010

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!

4 comments:

  1. JD,

    Don'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.

    ReplyDelete
    Replies
    1. 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?

      Delete
  2. I 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!

    function 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;
    }

    ReplyDelete