2011-11-29 21 views
29

Potrzebuję informacji dotyczących uruchamiania i zatrzymywania programatora w PHP. Potrzebuję zmierzyć czas, jaki upłynął od uruchomienia mojego programu .exe (Im używam funkcji exec() w moim skrypcie php), aż do zakończenia wykonywania i wyświetlenia czasu, który zajęł w sekundach. Czy istnieje sposób, w jaki mogę to zrobić.Uruchomienie i zatrzymanie zegara PHP

Dzięki

Odpowiedz

3

Można użyć klasy Timer

<?php 

class Timer { 

    var $classname = "Timer"; 
    var $start  = 0; 
    var $stop  = 0; 
    var $elapsed = 0; 

    # Constructor 
    function Timer($start = true) { 
     if ($start) 
     $this->start(); 
    } 

    # Start counting time 
    function start() { 
     $this->start = $this->_gettime(); 
    } 

    # Stop counting time 
    function stop() { 
     $this->stop = $this->_gettime(); 
     $this->elapsed = $this->_compute(); 
    } 

    # Get Elapsed Time 
    function elapsed() { 
     if (!$elapsed) 
     $this->stop(); 

     return $this->elapsed; 
    } 

    # Resets Timer so it can be used again 
    function reset() { 
     $this->start = 0; 
     $this->stop = 0; 
     $this->elapsed = 0; 
    } 

    #### PRIVATE METHODS #### 

    # Get Current Time 
    function _gettime() { 
     $mtime = microtime(); 
     $mtime = explode(" ", $mtime); 
     return $mtime[1] + $mtime[0]; 
    } 

    # Compute elapsed time 
    function _compute() { 
     return $this->stop - $this->start; 
    } 
} 

?> 
5

Dla celów, to proste Klasa powinna być wszystkim, czego potrzebujesz:

class Timer { 
    private $time = null; 
    public function __construct() { 
     $this->time = time(); 
     echo 'Working - please wait..<br/>'; 
    } 

    public function __destruct() { 
     echo '<br/>Job finished in '.(time()-$this->time).' seconds.'; 
    } 
} 


$t = new Timer(); // echoes "Working, please wait.." 

[some operations] 

unset($t); // echoes "Job finished in n seconds." n = seconds elapsed 
Powiązane problemy