Jump to content

[SOLVED] Calculate the age of a person?


JJohnsenDK

Recommended Posts

I suggest something like

 


$bday = strtodate(); //this would have their properly formatted text birthday in it and then it would change it into a timestamp
$diff = time() - $bday;

 

Then you would have to break diff down into what ever you wanted to show.

Link to comment
Share on other sites

Here's the age in many measurements.

function birthdateToAge($birthdate){
$age = array();
$now = time();
$age['seconds']	= $now-$birthdate;
$age['minutes']	= floor($age['seconds']/60);
$age['hours']	= floor($age['minutes']/60);
$age['days']	= floor($age['hours']/24);
$age['years']	= floor($age['days']/365);
return $age;
}

$age = birthdateToAge(strtotime("March 20, 1980"));
print $age['years'];

 

 

Link to comment
Share on other sites

The mathematical way:

 

<?php

function calc_age($date)
{
$seconds = time() - strtotime($date);
$year = 60*60*24*365.25;
return floor($seconds / $year);
}

echo calc_age("March 20 1969")."<br>"; //output- 37
echo calc_age("Jan 20 1980")."<br>"; //output- 27
echo calc_age("31.5.05"); //output- 1

?>

 

Orio.

Link to comment
Share on other sites

Now that I think about it, due to the nature of leap years (Every 4 years except in the 100 years, except for 1000 years, etc.) It might be more accurate to do the math with the formatted dates. I guess it depends on how detailed you're trying to get.

Link to comment
Share on other sites

I try jesirose's script

function birthdateToAge($birthdate){
$age = array();
$now = time();
$age['seconds']	= $now-$birthdate;
$age['minutes']	= floor($age['seconds']/60);
$age['hours']	= floor($age['minutes']/60);
$age['days']	= floor($age['hours']/24);
$age['years']	= floor($age['days']/365);
return $age;
}

$age = birthdateToAge(strtotime("March 20, 1980"));
print $age['years'];

it output

date born: 2003-02-05<br />

today: 2007-02-04<br />

old: 4

 

Is it OK?

 

Link to comment
Share on other sites

Now that I think about it, due to the nature of leap years (Every 4 years except in the 100 years, except for 1000 years, etc.) It might be more accurate to do the math with the formatted dates. I guess it depends on how detailed you're trying to get.

 

So you can use the number given here by google's calculator :)

 

Orio.

Link to comment
Share on other sites

i try Orio's way

<?php
function calc_age($date)
{
$seconds = time() - strtotime($date);
$year = 60*60*24*365.25;
return floor($seconds / $year);
}
echo date('Y-m-d'), "\n"; //output 2007-02-04
echo calc_age('2004-02-05'), "\n"; // output 3
echo calc_age('2005-02-05'); // output 1
?>

Is it OK?

Link to comment
Share on other sites

When I send that date to my function I get the correct years of:

Array

(

    [seconds] => 126184073

    [minutes] => 2103067

    [hours] => 35051

    [days] => 1460

    [years] => 3

)

 

Is your server time set to a different timezone?

Link to comment
Share on other sites

Because it's not yet been exactly three years. Those amounts are rounded down, that's what Floor does. You know by looking at it that if tomorrow is their birthday, they are not yet 4 years old. However, since we don't know the time they were born, we assume it was midnight, and when you round out the days, it turns out to be 1460. If you put in a different date such as 2003-03-05, you'll see the days go down, because it's not quite four years yet. Their age is still 3.

 

Link to comment
Share on other sites

Because it's not yet been exactly three years. Those amounts are rounded down, that's what Floor does. You know by looking at it that if tomorrow is their birthday, they are not yet 4 years old. However, since we don't know the time they were born, we assume it was midnight, and when you round out the days, it turns out to be 1460. If you put in a different date such as 2003-03-05, you'll see the days go down, because it's not quite four years yet. Their age is still 3.

 

no. the error is thet betwen this two date is 2004-02-29.

it cause wrong output

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.