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 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题