title: PHP hours and minutes between two times
body: <pre><code>$time1 = "14:45";
  $time2 = "15:55";
  list($hours, $minutes) = explode(':', $time1);
  $startTimestamp = mktime($hours, $minutes);
  
  list($hours, $minutes) = explode(':', $time2);
  $endTimestamp = mktime($hours, $minutes);
  
  $seconds = $endTimestamp - $startTimestamp;
  $minutes = ($seconds / 60) % 60;
  $hours = round($seconds / (60 * 60));
  
  echo "&lt;b&gt;$hours&lt;/b&gt; hours and&lt;b&gt;$minutes&lt;/b&gt; minutes&lt;/b&gt;";
  </code></pre>
  
  <p>This code works good if the variable time1 is lower than variable time2, but if the time1 is "23:35" and time2 is "01:40" it will output  "-22 hours and -55 minutes". </p>
  
  <p>How can I correct this so it still will output the hours and minutes between the both times even if the time1 is lower than time2 variable</p>
  
  <p><em>This code will take two times example "14:45" and "16:33" and calculate how many hours and minutes it is between each time, but it doesn't work if "time1" is lower than "time2" (time1 and time2 is variables)</em></p>
  
  <p><a href="http://codepad.org/QmFdN6RV" rel="nofollow">http://codepad.org/QmFdN6RV</a> </p>