anatak Posted March 23, 2006 Share Posted March 23, 2006 I want to calculate the age of some one.so I want to substract 2 dates and then keep the year.at this point I get the years of today's date and substract the year of the birthday date of the person.but that means that every body's age changes at new year.$now = date("Y-m-d");$bday = $Row["UserBirthday"]; $age = $now-$bday;is how I calculate the age of someoneI guess there is a way to substract two dates and round down to the year.so does anybody kno how I can get the right number of years difference ?anatak Quote Link to comment https://forums.phpfreaks.com/topic/5571-calculating-age/ Share on other sites More sharing options...
ober Posted March 23, 2006 Share Posted March 23, 2006 My suggestion would be to use strtotime() to reduce the 2 dates to the unix timestamp and do your calculations based off of that. Quote Link to comment https://forums.phpfreaks.com/topic/5571-calculating-age/#findComment-19960 Share on other sites More sharing options...
kenrbnsn Posted March 23, 2006 Share Posted March 23, 2006 Your code may not return anything meaningful:[code]<?php$now = date("Y-m-d");$bday = $Row["UserBirthday"];$age = $now-$bday;?>[/code]The value of "$now" is a string, the subtraction might work because PHP will automatically turn a string like "2006-03-23" into 2006.What you should do is something like:[code]<?php$hour = 3600; // number of seconds in an hour$day = hour * 24; //number of seconds in a day (86400)$yr = $day * 365.25; // number of seconds in a year, approximately$now = strtotime('today'); // the number of seconds since 1-1-1970$bday = strtotime($Row['UserBirthday']); // I'm assuming that the format of $Row['UserBirthday'] is yyyy-mm-dd$age = floor($now - $bday)/$yr;echo $age;?>[/code]This algorithm will work all the time if the birthday is on or after 1-1-1970. On some systems it might work for earlier dates.Ken Quote Link to comment https://forums.phpfreaks.com/topic/5571-calculating-age/#findComment-19966 Share on other sites More sharing options...
anatak Posted March 30, 2006 Author Share Posted March 30, 2006 here is my solution.because of the problem wiht the range of dates I wrote the following functionhere is the code that calls the function age()[code]If (substr($Row["UserBirthday"], 0, 4)=="0000"){ $date=display_date_format($Row['UserBirthday']); $bday=substr($date, 0,strlen($date)-4).'secret'; $age='secret';}else{ $today = date("Y-m-d"); $bday = $Row["UserBirthday"]; $age= age($today, $bday); echo "function age: ".$age;// $age = $today-$bday; $bday = display_date_format($Row["UserBirthday"]);}[/code]and here is the function age()[code]function age($arg01, $arg02){ $today=$arg01; $bday=$arg02; echo $today ." today<br>"; echo $bday . " birthday<br>"; $todayyear=substr($today, 0, 4); $todaymonth=substr($today, 5, 2); $todayday=substr($today, 8, 2); $bdayyear=substr($bday, 0, 4); $bdaymonth=substr($bday, 5, 2); $bdayday=substr($bday, 8, 2); $age = $today - $bdayyear; if($todaymonth-$bdaymonth<=0){ echo $age." month age -1<br>"; $age--; }elseif($todayday-$bdayday<0){ echo $age." day age -1<br>"; $age--; } return $age;}[/code]this works for methank you for looking into my problemanatak Quote Link to comment https://forums.phpfreaks.com/topic/5571-calculating-age/#findComment-22242 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.