毫秒到秒或小时或分钟的转换 [英] microtime to seconds or hours or min conversion
问题描述
我在数据库中存储的开始时间为microtime(),结束时间为microtime().
I have the store start time as microtime() and end time as microtime() into database.
现在,我想计算脚本执行秒/分钟/小时所花费的时间.
Now I want to calculate how long the script takes the seconds/minutes/hours to execute.
如何使用PHP?
推荐答案
基本上是这样的:
echo date("H:i:s",$endtime-$starttime);
$endtime-$starttime
给出持续时间(以秒为单位),使用日期格式化输出.听起来像是保存到数据库或从中读取不是您的问题,因此我在示例中忽略了这一点.请注意,您必须使用microtime(true)
才能使它正常工作,由于microtime()
的空格分隔输出,您无法轻松地进行计算.
$endtime-$starttime
gives the duration in seconds, date is used to format the output. sounds like saving to and reading from a database isn't your problem here, so i left that out in my example. Note that you'll have to use microtime(true)
to get this working, with the space-seperated output of microtime()
your can't do calculations that easy.
编辑:您也可以自行进行所有计算.只是这样的基本数学:
you could do all the calculation on your own, too. it's just basic math like this:
$duration = $endtime-$starttime;
$hours = (int)($duration/60/60);
$minutes = (int)($duration/60)-$hours*60;
$seconds = (int)$duration-$hours*60*60-$minutes*60;
这篇关于毫秒到秒或小时或分钟的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!