用PHP转换12小时到24小时

时间:2015-10-24 05:19:45

标签: php

这是我的作业,我被要求将时间从12小时转换为24小时,以这种格式05:09:15AM提供时间。我是编程的新手,这就是为什么不是进入循环而是我决定用条件语句来做。所以,我创建了4个条件(我使用条件为shown here

那么问题是什么?问题是我在打印$_time variable is undefined时收到错误,指出$_time。根据我的理解,这种情况正在发生,因为$_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;

?>

4 个答案:

答案 0 :(得分:2)

在您的代码中,几乎没有错误。 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;时它未定义。但是在初始化中初始化变量的良好做法。

答案 1 :(得分:1)

您有内置函数来转换日期时间对象。你可以参考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;
?>

答案 2 :(得分:0)

要点仅是更改小时值,如果PM,则加+12,如果AM,则更改为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);
?>

答案 3 :(得分:-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
相关问题