Start and stop a timer PHP

前端 未结 8 741
北海茫月
北海茫月 2020-11-30 08:08

I need some information regarding starting and stopping a timer in PHP. I need to measure the elapsed time from the start of my .exe program (I\'m using exec()

相关标签:
8条回答
  • 2020-11-30 08:46

    Use the microtime function. The documentation includes example code.

    0 讨论(0)
  • 2020-11-30 08:47

    For your purpose, this simple class should be all you need:

    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
    
    0 讨论(0)
  • 2020-11-30 08:49

    You can use microtime and calculate the difference:

    $time_pre = microtime(true);
    exec(...);
    $time_post = microtime(true);
    $exec_time = $time_post - $time_pre;
    

    Here's the PHP docs for microtime: http://php.net/manual/en/function.microtime.php

    0 讨论(0)
  • 2020-11-30 08:59

    You can use Timer Class

        <?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;
       }
    }
    
    ?>
    
    0 讨论(0)
  • 2020-11-30 08:59

    As alternative, php has a built-in timer controller: new EvTimer().

    It can be used to make a task scheduler, with proper handling of special cases.

    This is not only the Time, but a time transport layer, a chronometer, a lap counter, just as a stopwatch but with php callbacks ;)

    EvTimer watchers are simple relative timers that generate an event after a given time, and optionally repeating in regular intervals after that.

    The timers are based on real time, that is, if one registers an event that times out after an hour and resets the system clock to January last year, it will still time out after(roughly) one hour.

    The callback is guaranteed to be invoked only after its timeout has passed (...). If multiple timers become ready during the same loop iteration then the ones with earlier time-out values are invoked before ones of the same priority with later time-out values.

    The timer itself will do a best-effort at avoiding drift, that is, if a timer is configured to trigger every 10 seconds, then it will normally trigger at exactly 10 second intervals. If, however, the script cannot keep up with the timer because it takes longer than those 10 seconds to do) the timer will not fire more than once per event loop iteration.

    The first two parameters allows to controls the time delay before execution, and the number of iterations.

    The third parameter is a callback function, called at each iteration.

    after
    
        Configures the timer to trigger after after seconds.
    
    repeat
    
        If repeat is 0.0 , then it will automatically be stopped once the timeout is reached.
        If it is positive, then the timer will automatically be configured to trigger again every repeat seconds later, until stopped manually.
    

    https://www.php.net/manual/en/class.evtimer.php

    https://www.php.net/manual/en/evtimer.construct.php

    $w2 = new EvTimer(2, 1, function ($w) {
        echo "is called every second, is launched after 2 seconds\n";
        echo "iteration = ", Ev::iteration(), PHP_EOL;
    
        // Stop the watcher after 5 iterations
        Ev::iteration() == 5 and $w->stop();
        // Stop the watcher if further calls cause more than 10 iterations
        Ev::iteration() >= 10 and $w->stop();
    });
    

    We can of course easily create this with basic looping and some tempo with sleep(), usleep(), or hrtime(), but new EvTimer() allows cleans and organized multiples calls, while handling special cases like overlapping.

    0 讨论(0)
  • 2020-11-30 09:01

    Also you can use HRTime package. It has a class StopWatch.

    0 讨论(0)
提交回复
热议问题