wilna Posted May 16, 2011 Share Posted May 16, 2011 Hi I have a date of birth field(dob) in my table, eg 16/05/2011 (dd/mm/yyyy). I need to run a query but it must not include the year, how do I do that? $sql = "SELECT * FROM detail WHERE dob='$mydate'"; The dob is in the format dd/mm/yyyy, how do I change my query to just use dd/mm? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/236525-php-date-field/ Share on other sites More sharing options...
Adam Posted May 16, 2011 Share Posted May 16, 2011 The dob is in the format dd/mm/yyyy, how do I change my query to just use dd/mm? You'll need to use the proper 'date' data type first, which will store the date in the format "YYYYMMDD". Quote Link to comment https://forums.phpfreaks.com/topic/236525-php-date-field/#findComment-1215964 Share on other sites More sharing options...
wilna Posted May 16, 2011 Author Share Posted May 16, 2011 Hi, my field is in date format, please see the attached examples. Can you please be so kind and as to explain how my query must look? [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/236525-php-date-field/#findComment-1215975 Share on other sites More sharing options...
cyberRobot Posted May 16, 2011 Share Posted May 16, 2011 You could break apart the date with explode(): list($datePart_day, $datePart_month, $datePart_year) = explode('/', $mydate); Then just use the new variables in the query: $sql = "SELECT * FROM detail WHERE dob LIKE '%$datePart_month-$datePart_day'"; Of course you may need to do some checking to make sure month and day are valid. And make sure they are padded with zeros if needed. Quote Link to comment https://forums.phpfreaks.com/topic/236525-php-date-field/#findComment-1215978 Share on other sites More sharing options...
Adam Posted May 16, 2011 Share Posted May 16, 2011 @cyberRobot It would be better to use the built-in MySQL functions for this, to avoid splitting the logic and it's a lot more efficient than using a LIKE statement. @wilna Ah okay great - though that wasn't what you said originally. Anyway you want to use the DATE() and MONTH() functions to do this: select * from table where date(dob) = 16 and month(dob) = 5; Quote Link to comment https://forums.phpfreaks.com/topic/236525-php-date-field/#findComment-1215995 Share on other sites More sharing options...
cyberRobot Posted May 16, 2011 Share Posted May 16, 2011 @MrAdam - Ah, yes that does look much easier. Of course, they'll still need to use something like explode() to break apart the date contained in $mydate. Quote Link to comment https://forums.phpfreaks.com/topic/236525-php-date-field/#findComment-1216004 Share on other sites More sharing options...
wilna Posted May 16, 2011 Author Share Posted May 16, 2011 Thank you both! The following solved my problem: $myDate = date('d'); $myDate1 = date('m'); ..... $sql = "SELECT * FROM detail WHERE month(dob)='$myDate1'and day(dob)='$myDate'"; Quote Link to comment https://forums.phpfreaks.com/topic/236525-php-date-field/#findComment-1216057 Share on other sites More sharing options...
Adam Posted May 16, 2011 Share Posted May 16, 2011 day(dob)='$myDate' Sorry day() yeah, not date(). Quote Link to comment https://forums.phpfreaks.com/topic/236525-php-date-field/#findComment-1216076 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.