1

如何转换这样的军事时间?

 07:03    -->   07:30
 06:45    -->   07:00
 07:36    -->   08:00

 19:15    -->   19:30
 18:35    -->   19:00
 19:35    -->   20:00

基本上问题是,如果时间间隔为 30 分钟,您将把剩余的分钟加起来,使其成为一个完整的时间。

在我的数据库中,我必须像这样比较时间:

function check_range($get_shift, $date, $time, $id, $mode) {
    $sql = "SELECT * FROM tbl_employee WHERE id = '$id'";
    $result = mysql_query($sql);
    $row_check_range = mysql_fetch_assoc($result);
    $getShift = $row_check_range['shift'];

    $sql2 = "SELECT * FROM tbl_shift WHERE shift = '$getShift'";
    $result2 = mysql_query($sql2);
    $row_check_range2 = mysql_fetch_assoc($result2);
    $getTimeShift = $row_check_range2['timeIn'];

    $actual_time_in_mode = explode(':', $time); //split time into (e.g 07, 11)
    if ($mode == 'AM') {
        $time_in_actual = $actual_time_in_mode[0];
    } else {
        $time_in_actual = $actual_time_in_mode[0];
        $time_shift = explode(':', $getTimeShift);
        $getTimeShift = $time_shift[0] + 12; //convert to military time
    }

    $getTimeShift = mktime($getTimeShift);
    $actual_in_mode = mktime($time);

    if ($actual_in_mode > $getTimeShift) // LATE
    {
        return $date.' '.$time_in_actual.':30:00';
    } else {
        $time_in_actual += 1;
        return $date.' '.$time_in_actual.':00:00';
    }
}
4

3 回答 3

1

这样的事情应该做。

list($hour, $minute) = explode(':', $time);

if ($minute > 30) {
    $hour++;
    $minute = 0;
} elseif ($minute > 0) {
    $minute = 30;
}

if ($hour == 24) $hour = 0;

printf('%02d:%02d', $hour, $minute);
于 2013-08-03T01:59:42.280 回答
0

只需添加 30 分钟,然后将数字向下舍入到最接近的值:30:00

于 2013-08-03T01:52:40.430 回答
0
function military_time($x){
$y=split(':',$x);
$y[0] =(((int)$y[1]) >30) ? ((int)$y[0])+1 : $y[0];
$y[0] =((int) $y[0] == 24) ? '00':$y[0];
$y[0]=(strlen($y[0]) == 1) ? '0'.$y[0] : $y[0];
$y[1] =(((int)$y[1]) >30) ? '00':'30';
echo $y[0].':'.$y[1];
}
military_time('23:35');
于 2013-08-03T02:14:14.287 回答