2011-10-12 91 views
2
class Test extends Controller {  
public function start_process(){ 
    $begin_time = time(); 
    echo $begin_time ; // suppose it shows 1318412333 
    // statement 1 
    // statement 2 
    // statement 3 
    // statement 4 
    // statement 5 
    // statement 6 
    // some more code 

    $this->end_process($begin_time); 
    } 

    public function end_process($begin_time){ 
    $end_time = time(); 
    echo $begin_time . " " . $end_time; 
    } 
} 

现在当我回声在end_process函数。假设end_time是1318412390,我得到的begin_time与end_time相同。为什么会发生这种情况..如何获取在第一个函数中设置的原始begin_timePHP开始时间和结束时间问题

+0

你的意思是回声1318412333 1318412390 1318412390? – Nin

+0

在开始和结束之间添加'sleep(1);'然后再次检查结果。 – xdazz

+0

你会的1318412333 1318412390 1318412390 – JAB

回答

3

尝试使用microtime。当执行时间少于一秒时,它为您提供更准确的时间读数。

编辑

microtime(true);将返回时间为浮动,以便更容易看出差别。

0

time();给你秒,但你实际处理的是它的分数。正如蜘蛛指出的,你想用microtime()!

<?php 

$start = microtime(true); 

// -- do something -- 

// calulcate duration between $start and now 
$duration = microtime(true) - $start; 
// print in the format 1.2345 for better reading 
printf("took %0.4d seconds", $duration); 
0

我做了一个快速测试:

<?php 
ini_set('display_errors', true); 
class test { 

    public function start_process(){ 
     $begin_time = time(); 
     echo $begin_time . "<br>"; // suppose it shows 1318412333 
     // statement 1 
     // statement 2 
     // statement 3 
     // statement 4 
     // statement 5 
     // statement 6 
     // some more code 
     sleep(1); 
     $this->end_process($begin_time); 
    } 

    public function end_process($begin_time){ 
     $end_time = time(); 
     echo $begin_time . " <br> " . $end_time; 
    } 
} 

$test = new test(); 
$test->start_process(); 

?> 

我的结果是

1318415493 
1318415493 
1318415494 

请发表您的代码。

0

只要运行该代码:

$test = new Test(); 
    $test->start_process(); 

    class Test {  
    public function start_process(){ 
     $begin_time = microtime(true); 
     echo $begin_time . ' '; // suppose it shows 1318412333 
     for($i=0;$i<10000;$i++) { 
      //just some delay as if the script is doing something 
     } 
     $this->end_process($begin_time); 
     } 

     public function end_process($begin_time){ 
     $end_time = microtime(true); 
     echo $begin_time . " " . $end_time; 
     } 
    } 

你会看到,是作品,所以这段代码是没有问题的。该错误必须在代码的其他部分。

+0

类似我确实同意你..这段代码它的工作..但你已经使用睡眠(1)在你的代码..这将创建时间差..如何获得时间差,而不使用睡眠().. – JAB

+0

啊,可以没有时间差(或小于一秒),然后使用microtime()如上所述。我将编辑我的代码,以便您可以看到 – Nin

相关问题