doupu2722 2013-08-03 01:49
浏览 35
已采纳

如何在PHP中将军事时间转换为整个时间?

How to convert military time like this?

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

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

Basically the thing is, if the time is in the interval of 30 mins, you will add up the remaining minutes to make it a whole time.

In my database, I have to compare time like this where:

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';
    }
}
  • 写回答

3条回答 默认 最新

  • duangou1953 2013-08-03 02:14
    关注
    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行