0

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;

?>
Vivek K.
  • 118
  • 14
  • if it's saying that `$_time` isn't defined, it's because in the current scope, it can't resolve the name `$_time`. try adding `$_time;` on a new line after `$_a = explode(':',$_a);` – iam-decoder Oct 24 '15 at 05:50
  • 1
    oh its not being defined because all of your conditionals are failing, anything seem wrong about: `strpos("PM", $_a[2] !== FALSE)` ?? it should be `strpos("PM", $_a[2]) !== FALSE` – iam-decoder Oct 24 '15 at 05:59
  • Thanks @iam-decoder for catching the error but that notice (error) persists – Vivek K. Oct 24 '15 at 06:07

4 Answers4

2

In your code there are few errors. The strpos syntax is wrong.

strpos("PM", $_a[2] !== FALSE) // this is incorrect

This you have to write

strpos($_a[2],"PM") //string first and search second.

This will return a integer, position of search string in the string, so don't use false instead use >-1

strpos($_a[2],"PM") > -1) //this is the correct method.

Also define $_time; in the starting and initialise it.

  <?php
    $_a = ("07:29:23PM");
    $_a = explode(':',$_a);
    $_time = "";                    //initialised the variable.
    if($_a[0] == 12 && $_a[1] <= 59 && strpos($_a[2],"PM") > -1)
    {
        $_rpl = str_replace("PM","",$_a[2]);
        $_time = $_a[0].":".$_a[1].":".$_rpl;
    }
    elseif($_a[0] < 12 && $_a[1] <= 59 && strpos($_a[2],"PM")>-1)
    {
        $_a[0] += 12;
        $_rpl = str_replace("PM","",$_a[2]);
        $_time = $_a[0].":".$_a[1].":".$_rpl;
    }
    elseif($_a[0] == 12 && $_a[1] <= 59 && strpos($_a[2],"AM" ) >-1)
    {
        $_a[0] = 00;
        $_rpl = str_replace("AM","",$_a[2]);
        $_time = $_a[0].":".$_a[1].":".$_rpl;
    }
    elseif($_a[0] < 12 && $_a[1] <= 59 && strpos( $_a[2],"AM")>-1)
    {
        $_rpl = str_replace("AM","",$_a[2]);
        $_time = $_a[0].":".$_a[1].":".$_rpl;
    }
    echo $_time;
    ?>

Actually, initialising variable was not causing the error. Error was in your strpos syntax, so none of the if condition was true, so no code executed, so while trying to echo $_time; it was undefined. But its good practice to initialise a variable in the starting itself.

Subin Thomas
  • 1,408
  • 10
  • 19
  • Answer Accepted. Thanks @Subin Thomas, that helped. But can you explain how initializing a variable eliminated the error? – Vivek K. Oct 24 '15 at 06:10
  • 1
    Actually, initialising variable was not causing the error. Error was in your strpos syntax, so none of the if condition was true, so no code executed, so while trying to echo $_time; it was undefined. But its good practice to initialise a variable in the starting itself. @Vivek – Subin Thomas Oct 24 '15 at 06:14
  • 1
    @SubinThomas this is the right answer, but please note that [`strpos()`](http://php.net/manual/en/function.strpos.php) will return `false` if the string it's searching for is not found in the string, using an arithmetic comparison will turn the boolean to 0 indicating that the string was indeed found, but it will look as though it was found at the beginning of the string, which is not the desired use here. you might have been thinking of javascript's `indexOf()` method :) – iam-decoder Oct 24 '15 at 06:49
1

You have inbuilt functions to convert the datetime objects. You can refer php manual for that.

If you want to convert manually, you can do like this.

<?php
$_a = ("10:29:23PM");
$_a = explode(':',$_a);
if(strpos( $_a[2],"PM") > -1)    //if PM given
{
$_a[2] = str_replace("PM","",$_a[2]);  //remove the PM 
if($_a[0] <12)          //if time less than 12
$_a[0] = $_a[0] + 12;  //then add 12 hours
}
if(strpos( $_a[2],"AM") > -1)   //if AM given
{
$_a[2] = str_replace("AM","",$_a[2]);  //remove AM 
if($_a[0]=='12')        //if 12 AM
$_a[0]='00';            //make it 0
}
$newtime = $_a[0].':'.$_a[1].':'.$_a[1];
echo $newtime;
?>
Subin Thomas
  • 1,408
  • 10
  • 19
0

The main point is only changing on hour value, adding +12 if PM and change to 00 if AM.

<?php

   $s       = "07:05:45PM";
   $s       = explode(':',$s);
   $time    = substr($s[2],2,4);
   $s[2]    = substr($s[2],0,2);
   
   if($time == "PM") {
       if($s[0] != "12")
        $s[0] = $s[0]+12;
   }

   if($time == "AM") {
       if($s[0] == "12")
        $s[0] = "00";
   }

    echo implode(":",$s);
?>
-1
$string="10:29:23PM";
$a=substr($string, 0, 8);
$b= substr($string, 8, 10);
$dates=$a." ".$b;
 // 12-hour time to 24-hour time 
 echo   $time_in_24_hour_format  = date("H:i:s", strtotime("$dates"));
    // 22:29:23 
 echo    $time_in_24_hour_format  = date("H:i", strtotime("$dates"));
    // 22:29
Hussy Borad
  • 626
  • 5
  • 20
  • 1
    the question was not "how do I do this?" it was, "why isn't this working?". also for your own endeavors, NEVER use `strtotime()` for conversions: [see why](http://sandbox.onlinephpfunctions.com/code/ac41f38490a9cf857fda9553af54ba5bdbfa8b5e) – iam-decoder Oct 24 '15 at 06:04