A timer, in PHP. You can (re)start, pause and stop. And get the passed time. With TimerCollection you can run multiple timers at once.
Currently supported time measuring options:
- Timer::DATETIME_TYPE: uses PHP's DateTime class (this is the default)
- Timer::MICROTIME_TYPE: uses PHP's microtime function
Using Composer
composer require davidbehler/timer
Without Composer
You can also download it from [Github] (https://github.com/davidbehler/timer), but no autoloader is provided so you'll need to register it with your own PSR-4 compatible autoloader.
Create a new timer with autostart
use DavidBehler\Timer\Timer;
$timer = new Timer;
Create a new timer with autostart and microtime option
$timer = new Timer(true, Timer::MICROTIME_TYPE);
Create a new timer without autostart
$timer = new Timer(false);
Manually start a timer
$timer = new Timer(false);
$timer->start();
Pause a timer
$timer = new Timer;
$timer->pause();
Stop a timer
$timer = new Timer;
$timer->stop();
Unpause a paused timer
$timer = new Timer;
$timer->pause();
$timer->start();
Restart a timer
$timer = new Timer;
$timer->restart();
Get duration of running timer in microseconds (uses current time)
$timer = new Timer;
usleep(1000);
$timer->getDuration(); // returns 0.001 (in a perfect world, but of course timings aren't this perfect)
Get duration of running timer in seconds with 4 digits after decimal point (uses current time)
$timer = new Timer;
usleep(555);
$timer->getDuration(true, 5); // returns 0.00055 (in a perfect world, but of course timings aren't this perfect)
Get duration of paused timer in seconds
$timer = new Timer;
usleep(500);
$timer->pause();
usleep(500);
$timer->getDuration(); // returns 0.0005 (in a perfect world, but of course timings aren't this perfect)
Get duration of stopped timer in seconds
$timer = new Timer;
usleep(500);
$timer->stop();
usleep(500);
$timer->getDuration(); // returns 0.0005 (in a perfect world, but of course timings aren't this perfect)
Get duration of timer paused/started multiple times in seconds
$timer = new Timer;
sleep(1);
$timer->pause();
usleep(500);
$timer->start();
sleep(2);
$timer->pause();
$timer->getDuration(); // returns 3.005 (in a perfect world, but of course timings aren't this perfect)
Get a report
$timer->getReport();
Create a new TimerCollection with the microtime option. All timers within this collection will use the measuring option the collection was inititialized with.
use DavidBehler\Timer\TimerCollection;
$timerCollection = new TimerCollection(Timer::MICROTIME_TYPE);
Start a time and get it's duration in seconds
$timerCollection->start('timer 1');
$timerCollection->getDuration('timer 1');
Start multiple timers at once and get their durations
$timerCollection->start('timer 1');
$timerCollection->start('timer 2');
$timerCollection->getDuration('timer 1');
$timerCollection->getDuration('timer 2');
// or
$timerCollection->start(array('timer 1', 'timer 2'));
$timerCollcetion->getDurations(array('timer 1', 'timer 2')); // returns an array of durations with timer labels as indeces
You can also stop, pause and restart multiple timers at once
$timerCollection->stop(array('timer 1', 'timer 2'));
$timerCollection->pause(array('timer 1', 'timer 2'));
$timerCollection->restart(array('timer 1', 'timer 2'));
You can get a list of all timers
$timerCollection->getTimers(); // returns an array with all setup timers
$timerCollection->getTimers(true); // returns an array with all the setup timers' labels
Get a single timer's report
$timerCollection->getReport('timer 4');
Get a report for multiple timers
$timerCollection->getReports(array('timer 5', 'timer 6'));