Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP将陆军时间转换为常规时间AM PM_Php_Time - Fatal编程技术网

用PHP将陆军时间转换为常规时间AM PM

用PHP将陆军时间转换为常规时间AM PM,php,time,Php,Time,我正在寻找一种使用AM/PM表达式将陆军时间(如23:00)转换为常规时间的方法。input$time的格式为'XXXX'(例如'0000'、'0001'、…、'2300')。我找不到这个函数,所以写了以下内容。想在这里发布,以防其他人也在寻找同样的东西 function convert_army_to_regular($time) { $hours = substr($time, 0, 2); $minutes = substr($time, 2, 2); if ($

我正在寻找一种使用AM/PM表达式将陆军时间(如23:00)转换为常规时间的方法。

input$time的格式为'XXXX'(例如'0000'、'0001'、…、'2300')。我找不到这个函数,所以写了以下内容。想在这里发布,以防其他人也在寻找同样的东西

function convert_army_to_regular($time) {
    $hours = substr($time, 0, 2);
    $minutes = substr($time, 2, 2);

    if ($hours > 12) { 
        $hours = $hours - 12;
        $ampm = 'PM';
    } else {
        if ($hours != 11) {
            $hours = substr($hours, 1, 1);
        }
        $ampm = 'AM';
    }
    return $hours . ':' . $minutes . $ampm;
}


只需在
strotime
函数中传递时间,如下所示:

$time_in_12_hour_format = date("g:i a", strtotime("23:00"));
echo $time_in_12_hour_format;

我认为“陆军时间”更通俗地称为“
:-p
$time24='23:00'$dt=新日期时间($time24);echo$dt->format('h:ia')@marabutt-我认为在允许你对海报发誓之前,你必须在这里有至少1K的代表。重复:从循环中尝试这个:echo date(“g:ia”,strotime($result['hourOpeningHour']))可能最好将时间转换为unix时间戳,然后使用
date
,这是非常灵活的。不过,+1。显而易见的答案是将其转换为unix时间戳或DateTime对象,然后使用date()或DateTime format()方法。。。更干净更容易这就是你要找的我的朋友。。
$time_in_12_hour_format = date("g:i a", strtotime("23:00"));
echo $time_in_12_hour_format;