title: Trouble using MSSQL datetime in PHP (via SQLSRV)
body: <p>I am going round in circles with this one! I'm doing the following:</p>
  
  <ol>
  <li>Retrieving a date from an MSSQL datetime field via SQL/PHP</li>
  <li>Sending the date to a new PHP page via the querystring</li>
  <li>Trying to use that date in a new SQL query</li>
  </ol>
  
  <p>I'm hitting problems here.</p>
  
  <p>If I use echo:</p>
  
  <pre><code>echo $_REQUEST['rSessionDate'];
  output: 15/10/2012
  </code></pre>
  
  <p>Which is fine, but when I use it in a SQL query I'm not getting the results I expect, so I thought the best thing to do would be to make sure it's being recognised as a date first.</p>
  
  <p>If I use date_format():</p>
  
  <pre><code>echo date_format($_REQUEST['rSessionDate'],'Y-m-d');
  output: Warning: date_format() expects parameter 1 to be DateTime, string given in ...
  </code></pre>
  
  <p>If I use strtotime():</p>
  
  <pre><code>echo strtotime($_REQUEST['rSessionDate']);
  output: (nothing)
  </code></pre>
  
  <p>If I use date():</p>
  
  <pre><code>echo date('Y-m-d H:i',$_REQUEST['rSessionDate']);
  output: Notice: A non well formed numeric value encountered in ...
  </code></pre>
  
  <p>If I use date() with strtotime():</p>
  
  <pre><code>echo date('Y-m-d H:i',strtotime($_REQUEST['rSessionDate']));
  output: 1970-01-01 01:00
  </code></pre>
  
  <p>I'm sure I'm totally missing something simple.</p>
  
  <p>EDIT: I've tried a few new functions I found:</p>
  
  <pre><code>$rSessionDate = new DateTime($_REQUEST['rSessionDate']);
  echo $rSessionDate-&gt;format('Y-m-d H:i:s');
  output: Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (15/10/2012) at position 0 (1): Unexpected character'
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>$rSessionDate = date_create($_REQUEST['rSessionDate']);
  echo date_format($rSessionDate, 'Y-m-d H:i:s');
  output: Warning: date_format() expects parameter 1 to be DateTime, boolean given i
  </code></pre>
  
  <p>EDIT 2:</p>
  
  <p>I have tried using CAST:</p>
  
  <pre><code>SELECT fCourseCode ,fCourseTitle FROM tCourses WHERE fCourseCode = '4B' AND (ISNULL(fValidStart, 0) &lt;= CAST('2012-10-15 00:00:00' as DATETIME)) 
  </code></pre>
  
  <p>But this fails with error "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value"</p>