php 微时到秒或小时或分钟的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6468127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:28:06  来源:igfitidea点击:

microtime to seconds or hours or min conversion

php

提问by svk

I have the store start time as microtime() and end time as microtime() into database.

我将商店开始时间作为 microtime() 和结束时间作为 microtime() 进入数据库。

Now I want to calculate how long the script takes the seconds/minutes/hours to execute.

现在我想计算脚本执行需要多长时间的秒/分/小时。

How can I do in PHP?

我可以在 PHP 中做什么?

回答by oezi

basically, like this:

基本上,像这样:

echo date("H:i:s",$endtime-$starttime);

$endtime-$starttimegives the duration in seconds, dateis 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.

$endtime-$starttime以秒为单位给出持续时间,日期用于格式化输出。听起来像在数据库中保存和读取不是你的问题,所以我在我的例子中忽略了这一点。请注意,您必须使用microtime(true)才能使其正常工作,因为空格分隔的输出microtime()不能那么容易地进行计算。

EDIT:you coulddo 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;

回答by user2754369

microtime to seconds or hours or min conversion?

微时间到秒或小时或分钟的转换?

microtime is the name of the php function that return a measure of time in microseconds and basically microseconds can be converted to:

microtime 是 php 函数的名称,它以微秒为单位返回时间度量,基本上微秒可以转换为:

    1 milliseconds = 1,000 microseconds
    1 second       = 1,000,000 microseconds
    1 minute       = 60,000,000 microseconds
    1 hour         = 3,600,000,000 microseconds


    or


    1 microsecond = 0.001 milliseconds
    1 microsecond = 0.000001 seconds
    1 microsecond = 0.0000000166666667 minutes
    1 microsecond = 0.000000000277777778 hours

回答by ahmed reda

function formatPeriod($endtime, $starttime)
{

  $duration = $endtime - $starttime;

  $hours = (int) ($duration / 60 / 60);

  $minutes = (int) ($duration / 60) - $hours * 60;

  $seconds = (int) $duration - $hours * 60 * 60 - $minutes * 60;

  return ($hours == 0 ? "00":$hours) . ":" . ($minutes == 0 ? "00":($minutes < 10? "0".$minutes:$minutes)) . ":" . ($seconds == 0 ? "00":($seconds < 10? "0".$seconds:$seconds));
}

回答by Lightness Races in Orbit

If you have a time Aand a time B, in seconds, then the number of seconds between those two absolute times is:

如果你有一个 timeA和一个 time B,以秒为单位,那么这两个绝对时间之间的秒数是:

B - A

If you want to format this number of seconds, you can use dateto prettify it.

如果要格式化这个秒数,可以使用date美化它。