Confused about date formats between PHP and MySQL

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.

Thanks.
alicia1234Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mallcoreCommented:
Are your times in the table as unix timestamp?
0
kevin_uCommented:



The simplest way to do date ranges is to add:

23:59:59 to end date strings when you pass it to the mysql query.

So if the post shows:
2009/06/30 ... it becomes 2009/06/30 23:59:59

There are many other ways.

Generally speaking you have to deal with this stuff in an application specific way.

Making both the user and php happy is somtimes a task.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alicia1234Author Commented:
No they are "datetime".

I "kluged" the "to" date by adding " 23:59:59" to the end of it, and that now works correctly, but, again, it's not very "elegant". ;-)

0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

kevin_uCommented:
any other solutions generally require more code.
0
alicia1234Author Commented:
So my basic approach is ok, given that the dates are "datetime" in MySQL? My code is below. I didn't include the functions here but you'll get the gist. The code does work.

The query is:

$query =  "SELECT * FROM tblSurveys WHERE ( (surveydate >= '{$startdate}') && (surveydate <= '{$enddate}') ) ORDER BY surveydate DESC";  

if (	isset($_POST['startdate']) && $_POST['startdate'] != '') {
			if (isDate($_POST['startdate'])) {
				$startdate = datestring_to_MySQL($_POST['startdate']);
			} else {	
			$message = ERR_INVALID_DATE; 
			}
		} else {
			$startdate = '1950-01-01'; //a long time ago!
		}

Open in new window

0
alicia1234Author Commented:
oops - my code got cut off ... here it is again:

if (isset($_POST['startdate']) && $_POST['startdate'] != '') {
if (isDate($_POST['startdate'])) {
$startdate = datestring_to_MySQL($_POST['startdate']);
} else {	
$message = ERR_INVALID_DATE; 
}
} else {
$startdate = '1950-01-01'; //a long time ago!
}
 
if (isset($_POST['enddate']) && $_POST['enddate'] != '') {
if (isDate($_POST['enddate'])) {
$enddate = datestring_to_MySQL($_POST['enddate']);
$enddate .= ' 23:59:59'; 
} else {	
$message = ERR_INVALID_DATE; 
}
} else {
$enddate = '2099-12-31'; //a long time in the future
}

Open in new window

0
alicia1234Author Commented:
kevinu: While you were posting the 23:59:59 "kluge", I was doing just that. But good to hear that it's kind of the "accepted" way to do it.
0
kevin_uCommented:
Well, its my accepted way.... there may be arguments to the contrary.

You could store using the date data type instead of datetime.

There are combinations of functions (either php string stuff or date manipulation in php) that would allow you to store only the date (truncate the time) before inserting the data in the first place.

There combinations of functions to pull only the date from a datetime field (involves converting to a string, and truncating the string)

This may provide more insight.

http://dev.mysql.com/doc/refman/5.0/en/datetime.html


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.