<?
function year_from_date($date)
{
$now time();
$then strtotime($date);
$month date('m'$now) - date('m'$then);
$day date('d'$now) - date('d'$then);
if (
$month >= 1){
    
$diff date('Y'$now) - date('Y'$then);
    } elseif (
$month <= 0) {
            if (
$day >= 0){
                
$diff date('Y'$now) - date('Y'$then);
                } else {
                    
$today date('Y'$now);
                    
$today--;
                    
$diff $today date('Y'$then);
                }
            } else {
                
$today date('Y'$now);
                
$today--;
                
$diff $today date('Y'$then);
        }
return 
$diff;
}

//use it like this

$age year_from_date("1980-10-24");

echo 
$age;
?>
<?
function years_between($fyear$fmonth$fday$tyear$tmonth$tday)
{
  return 
abs((mktime 000$fmonth$fday$fyear) - mktime 000$tmonth$tday$tyear))/(60*60*24*365));
}

echo 
years_between("1982","10","20",date("Y"time()),date("m",time()),date("d"time()));
?>
a birthday to age php script - TranceAddict Forums - Technology & Gadget Zone
return to tranceaddict TranceAddict Forums Archive > Other > Technology & Gadget Zone

 
a birthday to age php script
View this Thread in Original format
Boomer187
I am finishing up this questionnaire for a research project we have going and I wanted to get peoples age by their birthday. I didn't find any working scripts using google but ended up solving the problem. And this seemed very easy to do in my mind, but I couldn't get it to work for a while. So I modified some existing script that didn't quite work. It had a problem with people who have not had their birthday yet, it counted them as a year older.


I didn't even think to ask help here when I was stumped. So instead I will post up the ugly, non noted, horribly written code I ended up with. Modify it if you want.

PHP:




these php colors suck :-/
alefort
Hmmm...gotta say, that is one NASTY looking function. I wrote a function like this ages ago, and it was simply one line. Here it is:

PHP:



This Outputs: 24.27 Years old for me.
Boomer187
thanks, I specialize in nasty scripts :p

I knew there are probably a million easier, nicer, more handy ones out there. But for some reason I can never think of one of them.


One of my attempts used something similar with mktime, but somehow it said i was 37 years old no matter what day I was born on. Obviously I messed up something besides the formulas.
CLICK TO RETURN TO TOP OF PAGE
 
Privacy Statement