在PHP中减去一个数字串(HH:MM:SS)的方法?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了在PHP中减去一个数字串(HH:MM:SS)的方法?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道PHP / CodeignITer中是否可以减去时间格式的值

例如:

$time1 = "12:45:03";
$time2 = "14:03:48";

$timelength = $time2- $time1;

有关代码示例的任何建议或链接吗?

解决方法

这就像

//function to convert seconds into hour:minute:second
    function sec2hms ($sec,$padHours = false) 
    {

        // start with a blank string
        $hms = "";

         // do the hours First: there are 3600 seconds in an hour,so if we divide
         // the total number of seconds by 3600 and throw away the remainder,we're
         // left with the number of hours in those seconds
         $hours = intval(intval($sec) / 3600); 

         // add hours to $hms (with a leading 0 if asked for)
         $hms .= ($padHours) 
         ? str_pad($hours,2,"0",STR_PAD_LEFT). ":"
         : $hours. ":";

         // dividing the total seconds by 60 will give us the number of minutes
         // in total,but we're interested in *minutes past the hour* and to get
         // this,we have to divide by 60 again and then use the remainder
         $minutes = intval(($sec / 60) % 60); 

         // add minutes to $hms (with a leading 0 if needed)
         $hms .= str_pad($minutes,STR_PAD_LEFT). ":";

        // seconds past the minute are found by dividing the total number of seconds
        // by 60 and using the remainder
        $seconds = intval($sec % 60); 

        // add seconds to $hms (with a leading 0 if needed)
        $hms .= str_pad($seconds,STR_PAD_LEFT);

       // done!
       return $hms;

     }

    $suBTracted_time = strtotime($time2) - strtotime($time1); //gives difference in seconds
    echo(sec2hms($subtracted_time));

功能sec2hmshttp://www.laughing-buddha.net/php/lib/sec2hms/

脚本宝典总结

以上是脚本宝典为你收集整理的在PHP中减去一个数字串(HH:MM:SS)的方法?全部内容,希望文章能够帮你解决在PHP中减去一个数字串(HH:MM:SS)的方法?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。