PHP - Age From Birthdate
I thought I'd never have to ask another date related question after the help I got from Pikachu earlier this month but I can't get this simple thing to work.
I have a table post for Date of Birth (Format YYYY-MM-DD) and a search form where you select From and To Numbers to find users between these ages. I have tried a few different things - this one works best, if I choose from any number and up til current time I get results but as soon as I set the script to use the selected variable I bomb. Also, somehow I got the script to only return the users with a DOB of 0000-00-00. Here is the current version (shortened to show the relevant parts): Code: [Select] $now = time(); //current timestamp $now=(date("Y-m-d")); if (isset($_POST['ageFr'])) //sets the stopdate for the search $ageFr = sanitizeString($_POST['ageFr']); $yearStop="date_sub($now, INTERVAL $ageFr YEAR)"; if (isset($_POST['ageTo'])) //sets startdate for the same search $ageTo = sanitizeString($_POST['ageTo']); $yearStart="date_sub($now, INTERVAL $ageTo YEAR)"; $query = "SELECT * FROM user_criteria WHERE sex='$sex' AND (age BETWEEN ($yearStart) AND ($yearStop))"; //The query When I echo like this: echo "$ageFr to $ageTo <br />"; echo "$yearStart - $yearStop"; echo "<br /> $query"; I get this: 18 to 101 date_sub(2012-01-24, INTERVAL 101 YEAR) - date_sub(2012-01-24, INTERVAL 18 YEAR) SELECT * FROM user_criteria WHERE sex='Female' AND (age BETWEEN (date_sub(2012-01-24, INTERVAL 101 YEAR)) AND (date_sub(2012-01-24, INTERVAL 18 YEAR))) Shouldn't that work? Am I making a big or a small mistake here? Pikachu? Similar Tutorials//calculate age $birthdate = "1978-04-26"; //birth date... actually being obtained from a database $today = date("Y-m-d H:i:s"); // The exact date $age = date_diff($str_birthday, $today); echo $age; I'd like a simple code to echo the age of someone with the mysql database information that's in their record. This doesn't work. I have no idea why. Nothing seems to work that I've found on the net. Please help. Thanks. How can I build a form with a drop down menu with the year, day, month? And then send that information to a database. If there is another/better option besides a drop down menu? If so, please tell me. The rest of the form is working fine, and all the inforamtion is going to the database - last name, first name, address, phone number, zip code, race,,,,,,,,,. I work for a not-for-profit medical office - we provide medical services to the local people who can not afford to go to a regular doctor. We need a simple way to keep track of patient information. One of the things we need to enter is the birthdate. Searching the forums provided a lot of results, but the words date and form are used so much, I can not find anything that helped me. I also looked through php.net, but can not find anything there. I have an advanced search from that has 20 fields and it works great however I'm having a problem with customising the age range search based on the $birthdate field (YYYY/MM/DD). The user inputs $agefrom and $ageto and then I don't know how to convert the birthdate field to an age to be able to echo only the users in that age range... <?php $cQuery = 'true and '; $aSearchcriteria = array(); //more code here...(other search input that works) if(isset($_POST['agefrom']) and trim($_POST['agefrom']) and strip_tags ($_POST['agefrom']) and mysql_real_escape_string ($_POST['agefrom'] )) if(isset($_POST['ageto']) and trim($_POST['ageto']) and strip_tags ($_POST['ageto']) and mysql_real_escape_string ($_POST['ageto'] )) { $cQuery .= "$birthdate BETWEEN '$agefrom' AND '$agefrom' and "; $aSearchcriteria[] = $agefrom; $aSearchcriteria[] = $ageto; } //more code here...(other search input that works) $data = mysql_query("SELECT * FROM table WHERE $cQuery order by name, surname") or die("SELECT Error: ".mysql_error()); // more code here // table echo here... ?> |