0

My datetime field is like this: Tue May 05 00:07:32 2015

The SQL Query that I am using in my php code is:

  Select * from logs WHERE Date BETWEEN '".$from."' AND  '".$to."'
  Date is my column name

$from and $to are the variables in php that store datetime, in the same format as the one in my database.

This query extracts wrong results, that is, it does not show me the results that is in my mentioned range. What could be the problem?

Suppose when I write:

   $from= Fri May 01 00:00:00.000 2015 
   $ to= Sun May 31 23:59:59.999 2015

It displays the result from 1st May to 4th May only, not till 31st May

Sarah Mandana
  • 1,474
  • 17
  • 43
  • What is this `'".$from."' AND '".$to."'`? – Giorgi Nakeuri May 21 '15 at 11:51
  • "Wrong results" is not specific enough to say anything about your problem. – Sergey Kalinichenko May 21 '15 at 11:51
  • 1
    Fix your database. All databases have native formats for date/time values. Do not store them as strings. Even worse, don't name a string representation as `Date`, and don't include a time component in something called a `Date`. Are you intentionally trying to confuse yourself and other database users? – Gordon Linoff May 21 '15 at 11:52
  • where are you creating this query, as it is now you are just checking if your date is between 2 strings, if you give us some example data and the result you are getting now we might be able to help (also the code where you are creating this query would be nice) – Jeremy C. May 21 '15 at 11:52
  • 2
    You need to give more information here. What results you are getting, what you're expecting, what DBMS? Also this looks like php variables are you using php? If so what code? – Christian Barron May 21 '15 at 11:53
  • I have edited everything – Sarah Mandana May 21 '15 at 11:59
  • @user1259768 Is it MySQL? – user4035 May 21 '15 at 12:03
  • 1.) Don't use strings for dates. 2.) Maybe look up `STR_TO_DATE` for handling the PHP string that you pass. That should do it. – Ryan_W4588 May 21 '15 at 12:20
  • You need correct formats for your dates! Example for between statement in MySQL: http://stackoverflow.com/a/3822668/951690 – Scoutman May 21 '15 at 12:33

0 Answers0