{"id":20678678,"url":"https://github.com/ayesh/php-timer","last_synced_at":"2025-06-27T19:32:42.920Z","repository":{"id":17340618,"uuid":"81698104","full_name":"Ayesh/php-timer","owner":"Ayesh","description":"A stop-watch and timer with start/stop/pause features and minimal human-friendly formatting.","archived":false,"fork":false,"pushed_at":"2025-02-02T11:03:20.000Z","size":121,"stargazers_count":22,"open_issues_count":1,"forks_count":2,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-06-23T02:14:49.315Z","etag":null,"topics":["php","php-timer","timer"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ayesh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-02-12T02:36:49.000Z","updated_at":"2025-04-02T01:41:16.000Z","dependencies_parsed_at":"2025-05-20T02:07:06.238Z","dependency_job_id":"ed13e37c-8728-498c-a346-c5915e797ffa","html_url":"https://github.com/Ayesh/php-timer","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/Ayesh/php-timer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ayesh%2Fphp-timer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ayesh%2Fphp-timer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ayesh%2Fphp-timer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ayesh%2Fphp-timer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ayesh","download_url":"https://codeload.github.com/Ayesh/php-timer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ayesh%2Fphp-timer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262319261,"owners_count":23293009,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","php-timer","timer"],"created_at":"2024-11-16T21:21:38.829Z","updated_at":"2025-06-27T19:32:42.876Z","avatar_url":"https://github.com/Ayesh.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Timer\n\n[![Latest Stable Version](https://poser.pugx.org/ayesh/php-timer/v/stable)](https://packagist.org/packages/ayesh/php-timer) [![License](https://poser.pugx.org/ayesh/php-timer/license)](https://packagist.org/packages/ayesh/php-timer)  [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Ayesh/php-timer/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Ayesh/php-timer/?branch=master) [![CI](https://github.com/Ayesh/php-timer/workflows/CI/badge.svg)](https://github.com/Ayesh/php-timer/actions)  [![codecov](https://codecov.io/gh/Ayesh/php-timer/branch/master/graph/badge.svg)](https://codecov.io/gh/Ayesh/php-timer) [![Too many badges](https://img.shields.io/badge/style-too_many-brightgreen.svg?style=toomany\u0026label=badges)](https://github.com/Ayesh/php-timer)\n\n## Synopsis\nA helper class to calculate how long a particular task took.\n\nThis class is similar to phpunit/php-timer, but not a fork, nor mimic its functionality.\n\n - Multiple timers by a given key.\n - Read the current time elapsed without stopping the timer.\n - Stop the timer, and continue from where it left (stopwatch).\n - Dead simple API with only 4 static methods.\n - 100% unit test coverage.\n - Gets you precise time in milliseconds (with options to convert to seconds)\n - Individual `Stopwatch` class for serialization and other use cases.\n\n## Prerequisites\n\n - PHP 8.2 or later.\n\n## Installing\n\nThe simplest way would be to install using [composer](https://getcomposer.org).\n\n```bash\n    composer require ayesh/php-timer\n```\n\n## Usage\n\nIt is pretty simple to use the timer, with all methods being static, and allowing only 4 methods.\n\n#### Start timer\n```php\n    \u003c?php\n    use Ayesh\\PHP_Timer\\Timer;\n    Timer::start();\n````\nThis starts the timer (actually keeps the current time stored) with the key `default`. Throughout the library, if you do not provide a specific key, this default key is used.\n\nAlternately, you can start the timer with a given key:\n```php\n    Timer::start('something');\n```\nOnce you start the time with a given key, you can use the same key to refer to that particular timer.\nYou can of course use PHP magic constants to make things easier:\n```php\n    Timer::start(__FUNCTION__);\n```\nAttempting to start the timer with a non-string key will throw a `\\TypeError` exception.\nYou can call the `start` method multiple times even if the timer has started. It will not reset the timer.\n\n#### Read timer\n\nAfter starting the timer, you can read the elapsed time at any time. Reading the time will not stop the timer. You can read the timer, do some expensive calculations, and read again to get the cumulative time.\n\n```php\n    Timer::read(); // Default timer.\n    Timer::read('default'); // Default timer.\n    Timer::read('something'); // Timer started with key \"something\".\n```\n\nAttempting to read a timer that is not started will throw an `\\LogicException` exception.\n\n##### Formats\n\nYou can pass a second argument to let this library make minimal processing for you:\n\n```php\n    Timer::read('something', Timer::FORMAT_PRECISE); // 0.10180473327637\n```\n\nSee the formats section below for the formats supported.\n\n#### Stop timer\n\nYou can stop the timer anytime as well. This makes the library store the stop time, and your further `Timer::read()` calls will always return the time it took between start and stop.\n\n```php\n    Timer::stop(); // Default timer.\n    Timer::stop('something'); // Timer started with key \"something\"\n```\n\nAttempting to stop a timer that is not started will throw an `\\LogicException` exception.\n\n#### Reset timer\n\nBy default, starting the timer after stopping it will continue it from where it left off. For example, if you have 3 seconds on the timer when you stop it, and start it again, the total time will start from 3 seconds. You can explicitly reset the timer to make it start from 0.\nResetting the timer will not make the timer start again. You need to explicitly start the timer again with a `Timer::start()` call.\n\n```php\n    Timer::reset(); // Default timer.\n    Timer::reset('something');\n    Timer::resetAll(); // Resets all timers.\n```\n\n## Formats\n\nCurrently, the following formats are provided:\n\n - `FORMAT_PRECISE`: Precise timer value, without rounding it. e.g. `0.10180473327637`\n - `FORMAT_MILLISECONDS`:  Time in milliseconds, rounded to 2 decimals.\n - `FORMAT_SECONDS`: Time in seconds, rounded to 3 decimals.\n - `FORMAT_HUMAN`: Time in human-readable format, for example `1.05 minutes`.\n\n## Examples\n\n#### Calculate the timer one-off:\n```php\n    \u003c?php\n    use Ayesh\\PHP_Timer\\Timer;\n\n    Timer::start();\n    // do your processing here.\n    $time = Timer::read('default', Timer::FORMAT_SECONDS);\n    echo \"Script took {$time} second(s)\";\n````\n\n#### Stop watch functionality, with stop-and-go timer calculated separately.\n\n```php\n    \u003c?php\n    use Ayesh\\PHP_Timer\\Timer;\n\n    Timer::start('full');\n\n    Timer::start('laps');\n    sleep(1);\n    Timer::stop('laps');\n\n    sleep(2); // This time is not calculated under 'laps'\n\n    Timer::start('laps');\n    sleep(1);\n    Timer::stop('laps');\n\n    echo Timer::read('full', Timer::FORMAT_SECONDS); // 4 seconds.\n    echo \"\u003cbr /\u003e\";\n    echo Timer::read('laps', Timer::FORMAT_SECONDS); // 2 seconds (1 + 1)\n````\n\n## Development and tests\n\nAll issues are PRs are welcome. Travis CI and PHPUnit tests are included. If you are adding new features, please make sure to add the test coverage.\n\n## Credits\nBy [Ayesh Karunaratne](https://aye.sh) and [contributors](https://github.com/Ayesh/php-timer/graphs/contributors).\n\nkthxbye\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fayesh%2Fphp-timer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fayesh%2Fphp-timer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fayesh%2Fphp-timer/lists"}