Vivek K.
Vivek K.

Reputation: 128

Convert 12 hours to 24 hours in PHP

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;

?>

Upvotes: 0

Views: 1947

Answers (4)

Chalid Ade Rahman
Chalid Ade Rahman

Reputation: 1

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);
?>

Upvotes: 0

Subin Thomas
Subin Thomas

Reputation: 1416

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.

Upvotes: 2

Hussy Borad
Hussy Borad

Reputation: 583

$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

Upvotes: -1

Subin Thomas
Subin Thomas

Reputation: 1416

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;
?>

Upvotes: 1

Related Questions