douqilai4263 2018-07-22 13:54
浏览 83
已采纳

如何从24小时制(HHMM)中减去分钟数?

I'm getting the opening and closing time of a store in HHMM format (0000 to 2359):

$openingtime = intval($open->time);
$closingtime = intval($close->time);

Then I get the current time in the same format:

$nowtime = intval(date("G") . date("i"));

I want to know whether the current time is between 15 minutes before the opening time, and 45 minutes before the closing time.

I have this, but it is inaccurate:

if(($nowtime >= ($openingtime - 15)) && ($nowtime <= ($closingtime - 45))){
    // current time is between 15 minutes before opening and 45 minutes before closing
}

If the time is 2300, then 2300 - 15 = 2285, which isn't a valid time.

How can I resolve this?

Also, I assume I need to do something at the overlap of the day (0000), but I'm not sure what I need to do there.

  • 写回答

2条回答 默认 最新

  • dsgawmla208057 2018-07-22 14:10
    关注

    You can create DateTime object like that:

    $open = date_create_from_format('Hi', '1000');
    $close = date_create_from_format('Hi', '1900');
    

    Then you can use DateTimeInterval

    $interval15 = new \DateInterval('P0Y0DT0H15M');
    $interval45 = new \DateInterval('P0Y0DT0H45M');
    

    Then you can sub it from closing time and from open:

    $now = new DateTime();
    if ($now >= $open->sub($interval15) && $now <= $close->sub($interval45)) {
        // logic
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真