Anfaenger Posted February 10, 2009 Share Posted February 10, 2009 Hi, I am new to php and try to compare two dates. From what I have read this is very easy, yet I am struggling to get it to work. I would like to compare a predefined month to a date field called id_date(YYYY-MM-DD) in my database and output a result according to the date. Here is what I came up with: $dmonth=date('Y-m', strtotime('last month')) This should return 2009-01 In the next step SELECT id FROM mytable WHERE date(id_date)='$dmonth' The result is zero although there should be results. I also tried just id_date=.. with the same result and strtotime(id_date) which produced an sql error Probably the id_date is not converted properly but I have no ideas how to achieve that. Any hints or tipps are greatly apreciated. Thanks alot! Quote Link to comment https://forums.phpfreaks.com/topic/144654-simple-date-comparison-strtotime/ Share on other sites More sharing options...
premiso Posted February 10, 2009 Share Posted February 10, 2009 SELECT id FROM mytable WHERE DATE_FORMAT('%Y-%c', id_date)='$dmonth' Should get you the results you want. MySQL DATE_FORMAT Quote Link to comment https://forums.phpfreaks.com/topic/144654-simple-date-comparison-strtotime/#findComment-759044 Share on other sites More sharing options...
Anfaenger Posted February 10, 2009 Author Share Posted February 10, 2009 THANK YOU SO MUCH!!!! It works perfect... Have to say that I tried DATE_FORMAT also before but apparently used the wrong format and it also returned nothing.....stupid is as stupid does. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/144654-simple-date-comparison-strtotime/#findComment-759059 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.