title: PHP Regex for Time options HH:MM optional SS and optional am/pm/AM/PM
body: <p>I need a regex pattern that will match the following times:</p>
  
  <ul>
  <li>09:15:42</li>
  <li>09:15:42 am (or AM or pm or PM)</li>
  <li>09:15</li>
  <li>09:15 am (or AM or pm or PM)</li>
  </ul>
  
  <p>Here's what I have so far, but it's not making the seconds optional:</p>
  
  <pre><code>'~^(0[0-9]|1[0-2]):([0-5][0-9])(:[0-5][0-9])\h*?([ap]m)$~i'
  </code></pre>
  
  <p>The code is:</p>
  
  <pre><code>$pattern = '~^(0[0-9]|1[0-2]):([0-5][0-9])(:[0-5][0-9])\h*?([ap]m)$~i';
  return preg_match($pattern, $time);
  </code></pre>
  
  <p>Thanks</p>