是否有方法将军队时间(如23:00)转换为AM/PM表达式的常规时间?
就像这样把时间传递给strtotime函数:
$time_in_12_hour_format = date("g:i a", strtotime("23:00"));
echo $time_in_12_hour_format;
使用日期:
$army_time_str = "23:00";
$regular_time_str = date( 'g:i A', strtotime( $army_time_str ) );
echo $regular_time_str;
输入$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;
}