Kingy Posted February 2, 2008 Share Posted February 2, 2008 in my user database the users have there date of birth stored as DD MM YYYY eg: (23 Mar 1986). What i was wondering is can i turn that into their age. eg the above example would come out as 21 would i need to explode the DD MM YYYY or could i just leave it as is? Quote Link to comment Share on other sites More sharing options...
Cep Posted February 2, 2008 Share Posted February 2, 2008 No you would not need to do this, almost all these actions can be found in the date and time functions in PHP. <?php $date = "23 Jan 1998"; $thisyear = date("Y"); $birthyear = date("Y", strtotime($date)); $age = $thisyear - $birthyear; echo $age; ?> Please note I have not done it completely for you. Quote Link to comment Share on other sites More sharing options...
Kingy Posted February 2, 2008 Author Share Posted February 2, 2008 i understand that i have to make it sort out which month and day it is just so it knows exactly how old they are, but i don't really no how to do that using ur example Quote Link to comment Share on other sites More sharing options...
Barand Posted February 2, 2008 Share Posted February 2, 2008 <?php $dob = '23 jan 1998'; echo calcAge ($dob); function calcAge ($dob) { $tob = strtotime($dob); $age = date('Y') - date('Y', $tob); return date('md') < date('md', $tob) ? $age-1 : $age; } ?> I recommend you store dates in a DATE type column (format yyyy-mm-dd). Your format looks pretty but is useless as a db date format. Quote Link to comment Share on other sites More sharing options...
laffin Posted February 2, 2008 Share Posted February 2, 2008 how about echo intval(time()-strtotime("1968-12-31"))/(60*60*24*7*52)); the formula time returns amt of seconds thus (secs per min* mins per hr * hrs per day * days per week * weeks per year); Quote Link to comment Share on other sites More sharing options...
Barand Posted February 2, 2008 Share Posted February 2, 2008 <?php echo intval((time()-strtotime('1968-02-04'))/(60*60*24*7*52)); // --> 40, should be 39 ?> Quote Link to comment Share on other sites More sharing options...
laffin Posted February 2, 2008 Share Posted February 2, 2008 LOL okay yer right, i think it's the leap years. anyone want to use julian calander functions instead? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted February 2, 2008 Share Posted February 2, 2008 okay yer right, i think it's the leap years. No. Example: Person is born at June 1st, 2000. The current year is 2008, so if you say 2008-2000 then you'll have calculated the persons age to be 8, but it has not yet been the person's 8th birthday this year, so the actual age is 7. Barand's calcAge() takes that into account. Quote Link to comment Share on other sites More sharing options...
laffin Posted February 2, 2008 Share Posted February 2, 2008 reason i suggest Julian dates, is cuz its based on days instead of seconds, and can go back to year 1 for calculations. Just showing diff methods Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 2, 2008 Share Posted February 2, 2008 Any method that just takes a difference between numbers (it does not matter if the numbers are in seconds or the day number of the year) will be off by one day for every leap year that has occurred since their DOB. This will mean that near the birthday, the calculation will be incorrect. The code that Barand posted is the definition of a person's age. It even works if your birthday is on February 29th. If a DOB is stored as a DATETIME in a database, you can do the same calculation in a SELECT query (there is an example in the "pets" tutorial section in the mysql manual.) Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 2, 2008 Share Posted February 2, 2008 obsidian gave me this one: http://phpsnips.com/snippet.php?id=7 Quote Link to comment Share on other sites More sharing options...
laffin Posted February 2, 2008 Share Posted February 2, 2008 That's why Juluan Calander functions are better than the norm, they take account for leap years in the equation. Julian was before the gregorian calander (what we use today), but julian calander was a lot more accurate. from way back in my C days, there used to be a julian date toolbox, that had all these nifty date functions. 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.