Confused about date formats between PHP and MySQL
Posted on 2009-07-06
I've been wrestling with this most of the day and have finally decided to ask for help. I have a form with two dates, a "from" date and a "to" date.
I've got the form set up to accept dates in either mm/dd/yyyy or mm-dd-yyyy format. That's all working fine. So basically I have my dates as strings with the month first, then the day, then the year.
How do I now use these dates in a MySQL query? Using as is just doesn't work. Probably because MySQL wants to see the dates as yyyy-mm-dd, right? Do I actually have to convert them to that format? I did that and it does seem to work, but it also feels "klugey" and I wonder if there is some better way?
Also, after I converted the dates to yyyy-mm-dd strings to use in the where clause, I still have a problem because, for example, my "to" date is 2009-05-21, and there is a date/time in the database that is 2009-05-21 11:14. When I do a "less than or equal to" on the "to" date, it doesn't include this date, probably because what it's using is a time of 00:00.
What's the right way to be doing these date comparisons? All I want to do is have the user put in a start and end date, and then query the database to include all records between the two dates, inclusive. So 06/01/2009 to 06/30/2009 would give everything in June.
And on another note, just a simple quick question: do you use != for not equal, or !== ? It seems I've used both and both seem to work.