graham23s Posted April 4, 2008 Share Posted April 4, 2008 Hi Guys, when i get the users birthyear from mysql i do a simple bit of math to get the users age: <?php // work out the users age // $year = date('Y'); $users_age = $year - $users_birth_year; ?> $users_birth_year contains 1979 for example so 2008 - 1979 = 28 but im trying to think of the best way to get exactly the users age, so when i turn 29 in june how to update the age then? thanks guys Graham Quote Link to comment Share on other sites More sharing options...
peytonrm Posted April 4, 2008 Share Posted April 4, 2008 It's not possible to do that with only the year. You would need to store a full date/time in the database and then get a full timestamp (e.g. date('Y-m-d H:i:s') in your code and subtract those two. Quote Link to comment Share on other sites More sharing options...
graham23s Posted April 4, 2008 Author Share Posted April 4, 2008 sorry i should have said i also store the users birthmonth and day to e.g 18 | 6 | 1979 Graham Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 4, 2008 Share Posted April 4, 2008 First of all, you should be storing dates in a DATE type field. Then you can find the age using a simple formula in a query - http://dev.mysql.com/doc/refman/5.0/en/date-calculations.html Quote Link to comment Share on other sites More sharing options...
graham23s Posted April 4, 2008 Author Share Posted April 4, 2008 Hi Mate, ah i see, the way i have set it i have: <?php print("<td class='form_style' align='left'><label><b>Birthdate:</b></label></td><td class='form_style' align='left'>\n"); print("<select name='birth_month'>\n"); foreach($birth_month as $key => $birth_month_value) { print("<option value='$key'>$birth_month_value</option>\n"); } print("</select>\n"); print("<select name='birth_day'>\n"); for($i = 1; $i <= 31; $i++) { print("<option value='$i'>$i</option>\n"); } print("</select>\n"); print("<select name='birth_year'>\n"); for($year = 1930; $year <= 2008; $year++) { print("<option value='$year'>$year</option>\n"); } print("</select>\n"); print("</td>\n"); ?> i have been individually storing the month,day,year into mysql seperately, instead of all in the 1 column whats the best way to do all 3 in the 1 column? just grab all 3 values then stitch them together before the mysql insertion: $dob = $bmonth . "-" . $bday . "-" . $byear; or is there a better way? thanks mate Graham Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted April 4, 2008 Share Posted April 4, 2008 heres a function i created that calculates the age of my users. $birth = mm/dd/yyyy ex: $birth = 01/04/1967 heres the function: <?php function age($birth){ $blown = explode("/", $birth); $year = date("Y"); $month = date("m"); $day = date("d"); $n_year = $year - $blown[2]; $n_month = $month - $blown[1]; $n_day = $day - $blown[0]; if($n_month < 0){ $age = $n_year - 1; } else{ if($n_day < 0){ $age = $n_year - 1; } else{ $age = $n_year; } } return $age; } $birth = "01/02/1967"; age($birth); ?> Quote Link to comment Share on other sites More sharing options...
quiettech Posted April 4, 2008 Share Posted April 4, 2008 Check the php manual on DateTime functions. Particularly the date_create() function which will create a DateTime object for you, you can then use for date/time arithmetics or for SQL insertion. A typical example would probably be: $dateobj = datecreate('08 23 1969'); You can test $dateobj for 'false' in case the data is invalid. EDIT: Alternatively you can also use: $timestamp = strtotime('08 23 1969'); It has the same effect and probably the best option for SQL insertion. However probably not cross-compatible with other databases not being served on UNIX. EDIT2: both functions use the British form of month, day, year. Corrected my own examples. Quote Link to comment Share on other sites More sharing options...
AndyB Posted April 5, 2008 Share Posted April 5, 2008 Whatever else you do, you can avoid lots of future pain by storing dates in an unequivocal format. Using the ISO yyyy-mm-dd for stored date format allows you flexibility and simplicity that will never exist if you choose some quaint local format for 'date'. Note that storing date in a rational format does not preclude displaying it in whatever weird form floats your boat. 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.