Accurax Posted March 12, 2007 Share Posted March 12, 2007 This might be a little complicated, In my database i have the date of birth of my members stored in 3 fields, 'year' 'month' ' day'. I'm using the following code throughout my site, to calculate the age of a given user at any point and echo it onto the page. <?php day = $row['day']; $month = $row['month']; $year = $row['year']; $birthday = $day."-".$month."-".$year; $today = date('d-m-Y'); $a_birthday = explode('-', $birthday); $a_today = explode('-', $today); $day_birthday = $a_birthday[0]; $month_birthday = $a_birthday[1]; $year_birthday = $a_birthday[2]; $day_today = $a_today[0]; $month_today = $a_today[1]; $year_today = $a_today[2]; $age = $year_today - $year_birthday; if (($month_today < $month_birthday) || ($month_today == $month_birthday && $day_today < $day_birthday)) { $age--; } echo "<strong> - Age: </strong>"; echo ' '; echo $age; ?> The problem I now have a search form with which i want to allow my users to search for other members within a given age raneg... 25-78 for example. The form has two fields, one for the uppper age, and one for the lower age limits, and i only want to return entries between this criteria. Can anyone give me some idea on how to achieve this? ...... a push in the right direction would be greatly appreciated Link to comment https://forums.phpfreaks.com/topic/42339-how-to-select-users-within-a-range-of-ages/ Share on other sites More sharing options...
monk.e.boy Posted March 12, 2007 Share Posted March 12, 2007 why don't you use the SQL type 'date' to store the DOB, then you'd have access to all the DATE functions in SQL. Your database is a bit weird. monk.e.boy Link to comment https://forums.phpfreaks.com/topic/42339-how-to-select-users-within-a-range-of-ages/#findComment-205398 Share on other sites More sharing options...
obsidian Posted March 12, 2007 Share Posted March 12, 2007 I agree 100% with monk.e.boy on this one. You need to use the MySQL datatypes for what they are intended. If you have too much data to do this, you may consider a workaround similar to this: SELECT * FROM userTable WHERE DATE(CONCAT(bday_year, '-', bday_month, '-', bday_day)) BETWEEN CURDATE() - INTERVAL 78 YEAR AND CURDATE() - INTERVAL 25 YEAR; Hope this helps. Link to comment https://forums.phpfreaks.com/topic/42339-how-to-select-users-within-a-range-of-ages/#findComment-205408 Share on other sites More sharing options...
Accurax Posted March 12, 2007 Author Share Posted March 12, 2007 ok, i can see the sense in what your saying here.... but how do i go about comparing the date stored in my database with the numbers (ages) selected by my users? Link to comment https://forums.phpfreaks.com/topic/42339-how-to-select-users-within-a-range-of-ages/#findComment-205409 Share on other sites More sharing options...
monk.e.boy Posted March 12, 2007 Share Posted March 12, 2007 I don't understand, in what way was obsidians answer lacking? monk.e.boy Link to comment https://forums.phpfreaks.com/topic/42339-how-to-select-users-within-a-range-of-ages/#findComment-205415 Share on other sites More sharing options...
Accurax Posted March 12, 2007 Author Share Posted March 12, 2007 lacking in the sense that A) i dont need a workaround and B) i didnt understand a word of what he said The way i see it, all i need to do is convert the dob's in my database into ages, and compare them to the values entered in the form. How i do this is what im trying to figure out. I'm not asking for a solution here, im more asking for help in getting my head around it and actually understanding how it works..... whats the point in doingf it if i just copy code from other people? Link to comment https://forums.phpfreaks.com/topic/42339-how-to-select-users-within-a-range-of-ages/#findComment-205422 Share on other sites More sharing options...
obsidian Posted March 12, 2007 Share Posted March 12, 2007 The way i see it, all i need to do is convert the dob's in my database into ages, and compare them to the values entered in the form. That's exactly what I was trying to help you do. Since you are storing the values of the month, day and year separately, you'll need to tell MySQL that it is a date you are after by combining the columns manually. To do this, you need to concatenate the values with hyphens to get it in the appropriate format for SQL to recognize. Then, using the DATE() function around it makes it that much more explicitly stated. Finally, using the INTERVAL subtraction, you can easily compare the date ranges you're after. Link to comment https://forums.phpfreaks.com/topic/42339-how-to-select-users-within-a-range-of-ages/#findComment-205436 Share on other sites More sharing options...
monk.e.boy Posted March 12, 2007 Share Posted March 12, 2007 lacking in the sense that A) i dont need a workaround and B) i didnt understand a word of what he said The way i see it, all i need to do is convert the dob's in my database into ages, and compare them to the values entered in the form. How i do this is what im trying to figure out. I'm not asking for a solution here, im more asking for help in getting my head around it and actually understanding how it works..... whats the point in doingf it if i just copy code from other people? Well, there is this cool tool called google, if you type things like 'mysql CURDATE' into it, it tells you the answer ;D But see how obsidian is casting your data format into a DATE then doing the calculation. You should just store it as a date. monk.e.boy Link to comment https://forums.phpfreaks.com/topic/42339-how-to-select-users-within-a-range-of-ages/#findComment-205447 Share on other sites More sharing options...
Accurax Posted March 12, 2007 Author Share Posted March 12, 2007 thanks i think i can figure something out Link to comment https://forums.phpfreaks.com/topic/42339-how-to-select-users-within-a-range-of-ages/#findComment-205450 Share on other sites More sharing options...
obsidian Posted March 12, 2007 Share Posted March 12, 2007 Hey, guys, I just removed the last few posts that were made. Not only were they completely unrelated to the topic, but they were not informative at all, and they could be (and were) taken as insulting. Please try to refrain from any personal attacks on the boards. I know we all have bad days sometimes, but we're all here to help out, so let's try to keep the spirit of help about us and not talk down to someone because they don't know something we do. Chances are, if we're willing to listen, every person on this board could tell us something we don't know, too Thanks for your help with this! Link to comment https://forums.phpfreaks.com/topic/42339-how-to-select-users-within-a-range-of-ages/#findComment-205478 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.