title: if statements in php using dates
body: <p>I want to write an IF statement based on two dates.<br>
  I have a MySQL database (of which I have no control over) for tests and exams taken.<br>
  One of the fields is labelled <code>ClientTime</code> and outputs the date and time the test was taken. When queried the typical output would read as such <code>Thu Aug 25 16:47:05 GMT+0100 2011</code></p>
  
  <p>The following all happens in a <code>while loop</code></p>
  
  <p>I’ve managed to convert this MySQL date using <code>strtotime()</code> to a d/m/Y format so the above would read 25/08/2011 and I can display a table with all the correct dates. </p>
  
  <p>I also have a php form that asks for the number of days that this test will expire after and this gets stored in a variable <code>$IMonths</code> (from the $_POST['IMonths'] passed from the form page) - the php code will be changed to Months once I have it working but I am using days for the purpose of testing – hence the variable passed being called $IMonths.</p>
  
  <p>I have also created a <code>$today = date("d/m/Y");</code> variable</p>
  
  <p>I can successfully create a new date using the following command <code>$Expiry = date('d/m/Y', strtotime($row['ClientTime'] . "+$IMonths days"));</code> and can display all results with both the test date and the expiry date.</p>
  
  <p>I then want to include an if-statement within my while-statement that says is <code>($Expiry&gt;$today)</code> but it’s not accurate – there are no errors and changing the number of days changes the results. </p>
  
  <p>My test database has 5 exams in with the following dates. </p>
  
  <pre><code>Taken 17/08/2011
  Taken 17/08/2011
  Taken 22/08/2011
  Taken 22/08/2011
  Taken 24/08/2011
  </code></pre>
  
  <p>If I set the expiry days to 1, I get all results back as expected:</p>
  
  <p>If I set it to 10 days I get two results</p>
  
  <pre><code>Taken 17/08/2011
  Taken 17/08/2011
  </code></pre>
  
  <p>I don’t get any of the others even though their date will be less than today's date (07/09/2011 (as I write this)) but they do expire in the month of September.</p>
  
  <p>If I set the expiry days to 15, I get one result that is one day in the future!</p>
  
  <pre><code>Taken 17/08/2011
  </code></pre>
  
  <p>I hope that makes sense. My code is as follows:</p>
  
  <pre><code>&lt;?php
  
  // Make The MySQL Server Connection
  mysql_connect("localhost", "root", "") or die(mysql_error());
  
  // Make The MySQL Database Connection
  mysql_select_db("questions") or die(mysql_error());
  
  $IQuizName = $_POST['IQuizName'];
  $IMonths = $_POST['IMonths'];
  
  $result = mysql_query("SELECT * FROM questions.resultsets 
                         WHERE QuizName='$IQuizName' 
                         AND PassFail='Pass'") or die (mysql_error());
  
  $today = date("d/m/Y");
  
  echo "&lt;table border='1'&gt;";
  echo "&lt;tr&gt; &lt;th align='left'&gt;Candidate Name &lt;/th&gt; &lt;th align='left'&gt;Pass / Fail&lt;/th&gt; &lt;th align='left'&gt;Date last Taken&lt;/th&gt; &lt;th align='left'&gt;Expires&lt;/th&gt; &lt;th align='left'&gt;Today&lt;/th&gt; &lt;/tr&gt;";
  
    while($row = mysql_fetch_array($result)){
  
    //START of expiry calculation
    $Expiry = date('d/m/Y', strtotime($row['ClientTime'] . "+$IMonths days"));
  
    if($Expiry&gt;$today){
  
      echo "&lt;tr&gt;&lt;td&gt;"; 
      echo $row['Candidate'];
      echo "&lt;/td&gt;&lt;td&gt;";  
  
      echo $row['PassFail'];
      echo "&lt;/td&gt;&lt;td&gt;"; 
  
      //Date Conversion
      echo date('d/m/Y', strtotime($row['ClientTime']));
      echo "&lt;/td&gt;&lt;td&gt;"; 
  
      echo $Expiry;
      echo "&lt;/td&gt;&lt;td&gt;"; 
  
      echo $today;
      echo "&lt;/td&gt;&lt;tr&gt;"; 
  
    };
  }
  
  echo "&lt;/table&gt;";
  echo "&lt;P&gt;";
  ?&gt;
  </code></pre>