blue-genie Posted May 17, 2010 Share Posted May 17, 2010 i have 2 fields that are stored as varchars that are going into the db like this 2010-05-17 00:00:00 now i'm trying to do a select statement that says bring me back anything that is valid today. so if startDate is 2010-05-17 00:00:00 and endDate is 2010-05-17 00:00:00 and now() is 2010-05-17 then it should be okay (not looking at time $result = mysql_query("SELECT* from gamedetails where Date('startDate') < now() and Date('endDate') >= now() and gameIsActive = '1'") or die ('Error: '.mysql_error ()); Quote Link to comment https://forums.phpfreaks.com/topic/202034-how-do-i-compare-dates-that-are-not-dates-without-the-minutes/ Share on other sites More sharing options...
jdavidbakr Posted May 17, 2010 Share Posted May 17, 2010 i have 2 fields that are stored as varchars that are going into the db like this 2010-05-17 00:00:00 now i'm trying to do a select statement that says bring me back anything that is valid today. so if startDate is 2010-05-17 00:00:00 and endDate is 2010-05-17 00:00:00 and now() is 2010-05-17 then it should be okay (not looking at time $result = mysql_query("SELECT* from gamedetails where Date('startDate') < now() and Date('endDate') >= now() and gameIsActive = '1'") or die ('Error: '.mysql_error ()); Use date(): if date(startDate) = date('2010-05-17') or date(now) = '2010-05-17' etc. That strips the time off of a date and returns just YYYY-MM-DD Quote Link to comment https://forums.phpfreaks.com/topic/202034-how-do-i-compare-dates-that-are-not-dates-without-the-minutes/#findComment-1059734 Share on other sites More sharing options...
jdavidbakr Posted May 17, 2010 Share Posted May 17, 2010 Oh, I just noticed you have them as varchar. Any reason why? Quote Link to comment https://forums.phpfreaks.com/topic/202034-how-do-i-compare-dates-that-are-not-dates-without-the-minutes/#findComment-1059735 Share on other sites More sharing options...
blue-genie Posted May 18, 2010 Author Share Posted May 18, 2010 i don't know how to do databases properly and i had to put this together myself from scratch (i'm a flash developer) - so I did what was the easiest for me at the time. I'm trying to not change it at this point because i'm sure there will be alot of repercussions. Quote Link to comment https://forums.phpfreaks.com/topic/202034-how-do-i-compare-dates-that-are-not-dates-without-the-minutes/#findComment-1059901 Share on other sites More sharing options...
Mchl Posted May 18, 2010 Share Posted May 18, 2010 There are likely to be more repercussions if you don't change it. You already store it in MySQL compatible format, so it is likely that no changes in application code will be necessary. All you need to do is to change datatype from VARCHAR to DATETIME. This will make all operations on dates much (MUCH) easier. Quote Link to comment https://forums.phpfreaks.com/topic/202034-how-do-i-compare-dates-that-are-not-dates-without-the-minutes/#findComment-1059916 Share on other sites More sharing options...
blue-genie Posted May 18, 2010 Author Share Posted May 18, 2010 i've decided to do that. (do things properly) have changed it to Date (not dateTime) and i've done this now which works but i'm sure is messy and laughable. $today = getdate(); $y = $today[year]; $m = $today[mon]; $d = $today[mday]; $compDateStr = "".$y."-".$m."-".$d.""; $result = mysql_query("SELECT* from gamedetails where startDate <= date('".$compDateStr."') AND endDate >= date('".$compDateStr."') AND gameIsActive = 1"); Quote Link to comment https://forums.phpfreaks.com/topic/202034-how-do-i-compare-dates-that-are-not-dates-without-the-minutes/#findComment-1059924 Share on other sites More sharing options...
blue-genie Posted May 18, 2010 Author Share Posted May 18, 2010 i'll wait a few days and mark as solved in case you guys want to give me more feedback. Quote Link to comment https://forums.phpfreaks.com/topic/202034-how-do-i-compare-dates-that-are-not-dates-without-the-minutes/#findComment-1059926 Share on other sites More sharing options...
Mchl Posted May 18, 2010 Share Posted May 18, 2010 Enter MySQL magic SELECT * FROM gamedetails WHERE CURDATE() BETWEEN startDate AND endDate AND gameIsActive = 1 Quote Link to comment https://forums.phpfreaks.com/topic/202034-how-do-i-compare-dates-that-are-not-dates-without-the-minutes/#findComment-1059927 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.