douzaipou3327 2011-02-15 22:24
浏览 229
已采纳

格式化datetime(0000-00-00 00:00:00)到00:00:00,剩余时间

I have a $time variable set some time in the future. In this format 2011-02-15 23:19:44. Now I need to set time remaining in this format 00:00:10.

If for example the $time is 1 hour, 3 minutes and 10 sec in the future. The format would be 01:03:10

  • 写回答

2条回答 默认 最新

  • dswu26846 2011-02-15 22:29
    关注

    You should look at using the DateTime and DateInterval classes.

    $futureDate = new DateTime($time);
    $currentDate = new DateTime();
    
    $dateDiff = $currentDate->diff($futureDate);
    
    echo $dateDiff->format('%H:%I:%S');
    

    Note: if the difference is more than 24 hours, then you should look at the %Y-%m-%d values or %a for total days.

    If you wanted to show any additional days as multiples of 24 hours then:

    printf('%d:%s', 
        $dateDiff->format('%a') * 24 + $dateDiff->format('%H'), 
        $dateDiff->format('%I:%S')
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器