php 在php中计算经过的时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7850259/
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
Calculate elapsed time in php
提问by Wilest
Hi All I'm trying to calculate elapsed time in php. The problem is not in php, it's with my mathematical skills. For instance: Time In: 11:35:20 (hh:mm:ss), now say the current time is: 12:00:45 (hh:mm:ss) then the time difference in my formula gives the output: 1:-34:25. It should actually be: 25:25
大家好 我正在尝试在 php 中计算经过的时间。问题不在于 php,而在于我的数学技能。例如:Time In: 11:35:20 (hh:mm:ss),现在说当前时间是:12:00:45 (hh:mm:ss) 那么我的公式中的时差给出了输出: 1 :-34:25。它实际上应该是:25:25
$d1=getdate();
$hournew=$d1['hours'];
$minnew=$d1['minutes'];
$secnew=$d1['seconds'];
$hourin = $_SESSION['h'];
$secin = $_SESSION['s'];
$minin = $_SESSION['m'];
$h1=$hournew-$hourin;
$s1=$secnew-$secin;
$m1=$minnew-$minin;
if($s1<0) {
$s1+=60; }
if($s1>=(60-$secin)) {
$m1--; }
if($m1<0) {
$m1++; }
echo $h1 . ":" . $m1 . ":" . $s1;
Any help please?
请问有什么帮助吗?
EDIT
编辑
Sorry I probably had to add that the page refreshes every second to display the new elapsed time so I have to use my method above. My apologies for not explaining correctly.
抱歉,我可能不得不补充一点,页面每秒刷新一次以显示新的经过时间,所以我必须使用上面的方法。我很抱歉没有正确解释。
回答by Crontab
This will give you the number of seconds between start and end.
这将为您提供开始和结束之间的秒数。
<?php
// microtime(true) returns the unix timestamp plus milliseconds as a float
$starttime = microtime(true);
/* do stuff here */
$endtime = microtime(true);
$timediff = $endtime - $starttime;
?>
To display it clock-style afterwards, you'd do something like this:
之后要以时钟样式显示它,您可以执行以下操作:
<?php
// pass in the number of seconds elapsed to get hours:minutes:seconds returned
function secondsToTime($s)
{
$h = floor($s / 3600);
$s -= $h * 3600;
$m = floor($s / 60);
$s -= $m * 60;
return $h.':'.sprintf('%02d', $m).':'.sprintf('%02d', $s);
}
?>
If you don't want to display the numbers after the decimal, just add round($s);
to the beginning of the secondsToTime()
function.
如果您不想显示小数点后的数字,只需添加round($s);
到secondsToTime()
函数的开头即可。
回答by Decent Dabbler
Using PHP >= 5.3
you could use DateTime
and its method DateTime::diff()
, which returns a DateInterval
object:
使用PHP >= 5.3
你可以使用DateTime
它的方法DateTime::diff()
,它返回一个DateInterval
对象:
$first = new DateTime( '11:35:20' );
$second = new DateTime( '12:00:45' );
$diff = $first->diff( $second );
echo $diff->format( '%H:%I:%S' ); // -> 00:25:25
回答by donutdan4114
Keep track of your time using the 'time()' function. You can later convert 'time()' to other formats.
使用“time()”函数跟踪您的时间。您可以稍后将 'time()' 转换为其他格式。
$_SESSION['start_time'] = time();
$end_time = time();
$end_time - $_SESSION['start_time'] = 65 seconds (divide by 60 to get minutes)
And then you can compare that to another value later on.
然后您可以稍后将其与另一个值进行比较。
Use microtime if you need millisecond detail.
如果您需要毫秒级的细节,请使用微时间。
回答by Duane Gran
You can implement the solutions shown, but I'm fond of using the phptimer class(or others, this wheel has been invented a few times). The advantage is that you can usually define the timer to be active or not, thereby permitting you to leave the timer calls in your code for later reference without re-keying all the time points.
您可以实现所示的解决方案,但我喜欢使用phptimer 类(或其他类,这个轮子已经发明了几次)。优点是您通常可以定义计时器是否处于活动状态,从而允许您将计时器调用留在代码中供以后参考,而无需重新键入所有时间点。