2011-10-12 49 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

相關問題