Convert army time to regular time AM PM in PHP [closed]

安稳与你 提交于 2020-01-10 04:07:11

问题


I'm looking for a way to convert army time (such as 23:00) to regular time with the AM/PM expression.


回答1:


Just pass your time in strtotime function like this:

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



回答2:


http://www.php.net/manual/en/function.date.php

$army_time_str = "23:00";
$regular_time_str = date( 'g:i A', strtotime( $army_time_str ) );
echo $regular_time_str;



回答3:


input $time is in the form of 'XXXX' (e.g. '0000', '0001',...,'2300'). i couldn't find a function for this so wrote the following. wanted to post here in case anyone else is looking for the same thing.

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


来源:https://stackoverflow.com/questions/17033889/convert-army-time-to-regular-time-am-pm-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!