title: Comparing dates in a MySQLi WHERE subclause
body: <p>I'm a newbie. I want to generate a report to count those MySQLi records whose timestamps lie between two dates. Each record has a <code>timestamp</code> field that is automatically generated on creation by MySQLi. When I view the timestamps in phpMyAdmin they appear in the following format <code>2015-12-31 19:25:51</code>. </p>
  
  <p>I have a PHP page with HTML form inputs using radio buttons to choose "Last 7 days", "Last 30 days", "since records began", etc... Based on this selection I have a Javascript that creates startdate and enddate variables as DATE objects and I use JQuery $.post to send these to another PHP page to perform the query.</p>
  
  <p>I'm getting stuck writing the SQL query. I'm trying something like </p>
  
  <pre><code>$startdate = $_POST["startdate"];
  $enddate = $_POST["enddate"];
  $con = mysqli_connect("localhost","username","password","db");
  if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $sql="SELECT * FROM dbtable WHERE timestamp BETWEEN $startdate AND $enddate";
  if ($result = mysqli_query($con,$sql)) {
      $count = mysqli_num_rows($result);
  }
  </code></pre>
  
  <p>The code works fine if I don't have a WHERE subclause (but of course simply counts all the records which is not what I'm looking for). So I know the problem lies within the WHERE subclause. I suspect the problem is that the MySQLi timestamp format is different from the JS DATE object format of the variables that I'm posting through. If I could convert both the timestamp and the JS variables to integers (perhaps representing milliseconds since 1 Jan 1970 or similar) then the comparison would work.</p>