A small timer class I wrote:
Timer.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | <?php class Timer { const MICROSECONDS = 0; const SECONDS = 1; private $startTime; private $interval = 0; private $units; public function __construct($units = Timer::MICROSECONDS) { $this->startTime = $this->timeAsMilliseconds(); $this->units = $units; } public function start() { $this->startTime = $this->timeAsMilliseconds(); } public function restart() { $this->start(); } public function elapsedTime() { $diff = $this->timeAsMilliseconds() - $this->startTime; if($this->units == Timer::MICROSECONDS) { return $diff; } else { return (int)($diff / 1000); } } public function setInterval($interval) { if($this->units == Timer::SECONDS) { $interval *= 1000; } $this->interval = $interval; } public function intervalElapsed() { return ($this->timeAsMilliseconds() - $this->startTime) > $this->interval; } private function timeAsMilliseconds() { return round(microtime(true) * 1000); } } |
TimerDemo.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | require_once('Timer.php'); // timer test, milliseconds $timer = new Timer(); $timer->start(); usleep(1000); echo $timer->elapsedTime() . 'ms'; // timer test, seconds $timer = new Timer(Timer::SECONDS); $timer->start(); usleep(1000); echo $timer->elapsedTime() . 's'; // interval test, milliseconds $timer = new Timer(); $timer->setInterval(2000); $timer->start(); usleep(1000); echo $timer->intervalElapsed() ? 'true' : 'false'; usleep(1200); echo $timer->intervalElapsed() ? 'true' : 'false'; // interval test, seconds $timer = new Timer(Timer::SECONDS); $timer->setInterval(2); $timer->start(); usleep(1000); echo $timer->intervalElapsed() ? 'true' : 'false'; usleep(1200); echo $timer->intervalElapsed() ? 'true' : 'false'; |


Posted in
Tags: 
