JJohnsenDK Posted February 4, 2007 Share Posted February 4, 2007 Hey Im trying to make a function which can calculate the age of a person from the persons birthday. Lets say im born 20. March 1980, then i want the function to calculate how old i am today. How do i that? Quote Link to comment Share on other sites More sharing options...
corbin Posted February 4, 2007 Share Posted February 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 4, 2007 Share Posted February 4, 2007 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']; Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted February 4, 2007 Author Share Posted February 4, 2007 Thanks to both for you... jesirose very nice function, thanks Quote Link to comment Share on other sites More sharing options...
Orio Posted February 4, 2007 Share Posted February 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 4, 2007 Share Posted February 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
sasa Posted February 4, 2007 Share Posted February 4, 2007 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? Quote Link to comment Share on other sites More sharing options...
Orio Posted February 4, 2007 Share Posted February 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
sasa Posted February 4, 2007 Share Posted February 4, 2007 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? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 4, 2007 Share Posted February 4, 2007 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? Quote Link to comment Share on other sites More sharing options...
Orio Posted February 4, 2007 Share Posted February 4, 2007 Made a little change, now this should work: <?php function calc_age($date) { $seconds = time() - strtotime($date); $year = 31556926; return floor($seconds / $year); } ?> Orio. Quote Link to comment Share on other sites More sharing options...
sasa Posted February 4, 2007 Share Posted February 4, 2007 3 * 365 = 1095 and 4* 365 = 1460 how years is 3? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 4, 2007 Share Posted February 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
sasa Posted February 4, 2007 Share Posted February 4, 2007 Made a little change, now this should work: <?php function calc_age($date) { $seconds = time() - strtotime($date); $year = 31556926; return floor($seconds / $year); } ?> Orio. it stil output same results Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 4, 2007 Share Posted February 4, 2007 Which are? What is the problem? Both Orio's and mine work fine as far as we can tell, so you need to show what's not working. Quote Link to comment Share on other sites More sharing options...
sasa Posted February 4, 2007 Share Posted February 4, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.