douping1581 2015-05-11 10:44
浏览 46
已采纳

如何以HH:MM:SS格式添加两个日期字符串?

I am saving the variables in the format of HH:MM:SS. I want to sum up several variables such as:

TotalTime += var1+var2

It gives me the result of 0, whats the right format for getting the sum as HH:MM:SS?

  • 写回答

1条回答 默认 最新

  • dongya767979565 2015-05-11 11:01
    关注

    This should work for you:

    Here I just converted the first date into a DateTime object and the second date I converted into a DateInterval object, which I then can add() to the first date.

    <?php
    
        $var1 = "12:23:01";
        $var2 = "05:22:45";
    
        $date = new DateTime($var1);
        list($hours, $minutes, $seconds) = explode(":", $var2);
        $interval = new DateInterval("PT" . $hours . "H" . $minutes . "M" . $seconds . "S");
    
        $date->add($interval);
        echo $date->format("H:i:s");
    
    ?>
    

    output:

    17:45:46
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度