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 05:09:15AM
. 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 shown here)
What's the problem then? The problem is I am getting an error stating $_time variable is undefined
when I am printing $_time
. As per my understanding, this is happening because the $_time
variable is inside the functions. But, if that's the case, can you guide me how to do this?
<?php
$_a = ("07:29:23PM");
$_a = explode(':',$_a);
if($_a[0] == 12 && $_a[1] <= 59 && strpos("PM", $_a[2] !== FALSE))
{
$_rpl = str_replace("PM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] < 12 && $_a[1] <= 59 && strpos("PM", $_a[2] !== FALSE))
{
$_a[0] += 12;
$_rpl = str_replace("PM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] == 12 && $_a[1] <= 59 && strpos("AM", $_a[2] !== FALSE))
{
$_a[0] = 00;
$_rpl = str_replace("AM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] < 12 && $_a[1] <= 59 && strpos("AM", $_a[2] !== FALSE))
{
$_rpl = str_replace("AM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
echo $_time;
?>