title: Convert 12 hours to 24 hours in PHP
body: <p>This is my homework in which I was asked to convert time from 12 hours to 24 hours where time was provided in this format <code>05:09:15AM</code>. I am new to programming, that's why instead of going into loops I decided to do it with conditional statements. So, I created 4 conditions (I used conditions as <a href="https://www.mathsisfun.com/time.html" rel="nofollow">shown here</a>) </p>
  
  <p>What's the problem then? The problem is I am getting an error stating <code>$_time variable is undefined</code> when I am printing <code>$_time</code>. As per my understanding, this is happening because the <code>$_time</code> variable is inside the functions. But, if that's the case, can you guide me how to do this?</p>
  
  <pre><code>&lt;?php
  $_a = ("07:29:23PM");
  $_a = explode(':',$_a);
  if($_a[0] == 12 &amp;&amp; $_a[1] &lt;= 59 &amp;&amp; strpos("PM", $_a[2] !== FALSE))
  {
      $_rpl = str_replace("PM","",$_a[2]);
      $_time = $_a[0].":".$_a[1].":".$_rpl;
  }
  elseif($_a[0] &lt; 12 &amp;&amp; $_a[1] &lt;= 59 &amp;&amp; strpos("PM", $_a[2] !== FALSE))
  {
      $_a[0] += 12;
      $_rpl = str_replace("PM","",$_a[2]);
      $_time = $_a[0].":".$_a[1].":".$_rpl;
  }
  elseif($_a[0] == 12 &amp;&amp; $_a[1] &lt;= 59 &amp;&amp; strpos("AM", $_a[2] !== FALSE))
  {
      $_a[0] = 00;
      $_rpl = str_replace("AM","",$_a[2]);
      $_time = $_a[0].":".$_a[1].":".$_rpl;
  }
  elseif($_a[0] &lt; 12 &amp;&amp; $_a[1] &lt;= 59 &amp;&amp; strpos("AM", $_a[2] !== FALSE))
  {
      $_rpl = str_replace("AM","",$_a[2]);
      $_time = $_a[0].":".$_a[1].":".$_rpl;
  }
  
  echo $_time;
  
  ?&gt;
  </code></pre>