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条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办