title: Using PHP & IMAP to get emails not already grabbed
body: <p>This is more of a question about best practices - I hope that's ok.</p>
  
  <p>I am using PHP and IMAP to grab emails from a Gmail account every 30 minutes. I only want the script to grab emails from 30 minutes ago, so it will never grab the same email twice.</p>
  
  <p>There is a "since" command in the search query:</p>
  
  <pre><code>imap_search($inbox, 'SUBJECT "Ticket #" SINCE "'.date("Y-m-d").'"');
  </code></pre>
  
  <p>But this will only take a date and not a time.</p>
  
  <p>I can technically do a loop through all emails and only grab emails that are 30 minutes away from the current time:</p>
  
  <pre><code>$email_time = strtotime($overview[0]-&gt;date);
  $current_time = strtotime('-30 minutes');
  if($email_time &gt;= $current_time) {
      ...
  }
  </code></pre>
  
  <p>I don't believe this to be a reliable solution though, because whose to say the emails aren't delayed, or what happens if the server goes down for a few minutes?</p>
  
  <p>Every time the cron job executes the email script, I can store the time in the database and use that time as a reference to check all emails since. But then I need to create a brand new table, just for one field?</p>
  
  <p>I'm thinking there must be a better solution....</p>