在PHP中将12小时转换为24小时


Convert 12 hours to 24 hours in PHP

这是我的作业,我被要求将时间从12小时转换为24小时,时间以这种格式05:09:15AM提供。我对编程很陌生,这就是为什么我决定用条件语句来代替循环。因此,我创建了4个条件(我使用的条件如下所示)

那有什么问题吗?问题是,当我打印$_time时,我得到了一个错误,说明$_time variable is undefined。根据我的理解,这是因为$_time变量在函数内部。但是,如果是这样的话,你能指导我怎么做吗?

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

在您的代码中有几个错误。strpos语法错误。

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

这个你必须写

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

这将返回一个整数,搜索字符串在字符串中的位置,所以不要使用false,而是使用>-1

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

也定义$_time;在开始和初始化它。

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

实际上,初始化变量并没有导致错误。错误是在你的strpos语法中,所以没有一个if条件为真,所以没有执行代码,所以在尝试echo $_time;时它是未定义的。但是最好的做法是在开始时初始化变量。

您有内置函数来转换datetime对象。你可以参考php手册。

如果你想手动转换,你可以这样做。

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

主要是改变小时值,如果下午加+12,如果上午改00。

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

$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