Zend - The PHP Company




Calculators

Add Code


Simple Birthdate-to-Age-at-Given-Or-Current-Date Conversion function  

Type: code fragment
Added by: yaragn1
Entered: 15/07/2001
Last modified: 07/12/2000
Rating: - (fewer than 3 votes)
Views: 7096
Simple Birthdate-Age function. Given three parameters (month, day, year), it will return a correct age as an integer, doesn't rely on Timestamp.

You can give it another three parameters as the date to compare it to, i.e. it will return the age at a given date. Presumes current date.

For sanity if given two-digit years presumes date was in 20th century. Hope you find it useful. Not exactly rocket science.


<?php
function getAgeByDate ($iMonth$iDay$iYear$nMonth=0$nDay=0$nYear=0) { 
    
// simple mm/dd/yy(yy) to age conversion.
    // Copyright (C)2001 Servability Ltd. Beerware!!
    
If (!$nMonth$nMonth date("m");
    If (!
$nDay$nDay date("d");
    If (!
$nYear$nYear date("Y");
    If (
$iYear 100) {
        
$iYear 1900 $iYear;
    }
    
$baseyear $nYear $iYear 1;
    If (
$iMonth $nMonth OR ($iMonth == $nMonth AND $iDay <= $nDay)) {
        
// had birthday
        
$baseyear++;
    }
            
    return 
$baseyear
}
?>


Usage Example


print getAgeByDate(5,26,1979) . " now <br>";
print getAgeByDate(5,26,79,2,5,2020) . " on 2nd May 2020<br>";


Rate This Script





Search



This Category All Categories