{"record":{"id":"2a8a146bcbb42b49","repo":"sebastianbergmann/php-timer","slug":"cannot-determine-time-at-which-the-request-started-because","errorCode":null,"errorMessage":"Cannot determine time at which the request started because $_SERVER['REQUEST_TIME_FLOAT'] is not available","messagePattern":"Cannot determine time at which the request started because \\$_SERVER\\['REQUEST_TIME_FLOAT'\\] is not available","errorType":"exception","errorClass":"TimeSinceStartOfRequestNotAvailableException","httpStatus":null,"severity":"error","filePath":"src/ResourceUsageFormatter.php","lineNumber":43,"sourceCode":"        'KB' => 1024,\n    ];\n\n    public function resourceUsage(Duration $duration): string\n    {\n        return sprintf(\n            'Time: %s, Memory: %s',\n            $duration->asString(),\n            $this->bytesToString(memory_get_peak_usage(true)),\n        );\n    }\n\n    /**\n     * @throws TimeSinceStartOfRequestNotAvailableException\n     */\n    public function resourceUsageSinceStartOfRequest(): string\n    {\n        if (!isset($_SERVER['REQUEST_TIME_FLOAT'])) {\n            throw new TimeSinceStartOfRequestNotAvailableException(\n                'Cannot determine time at which the request started because $_SERVER[\\'REQUEST_TIME_FLOAT\\'] is not available',\n            );\n        }\n\n        if (!is_float($_SERVER['REQUEST_TIME_FLOAT'])) {\n            throw new TimeSinceStartOfRequestNotAvailableException(\n                'Cannot determine time at which the request started because $_SERVER[\\'REQUEST_TIME_FLOAT\\'] is not of type float',\n            );\n        }\n\n        return $this->resourceUsage(\n            Duration::fromMicroseconds(\n                (1000000 * (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'])),\n            ),\n        );\n    }\n\n    private function bytesToString(int $bytes): string","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/sebastianbergmann/php-timer/blob/3efcdcf2132bd0125edb602c1363011dc5434c3b/src/ResourceUsageFormatter.php#L25-L61","documentation":"This exception is thrown by ResourceUsageFormatter::resourceUsageSinceStartOfRequest() when the superglobal $_SERVER['REQUEST_TIME_FLOAT'] is not set. The library uses that value (the timestamp the request started, provided by PHP's SAPI) to compute how much time/resources the request has consumed. Without it, no duration can be calculated, so the method fails fast with TimeSinceStartOfRequestNotAvailableException.","triggerScenarios":"Calling resourceUsageSinceStartOfRequest() in a context where PHP never populates $_SERVER['REQUEST_TIME_FLOAT'] — most commonly CLI scripts (phpunit, workers, cron jobs) instead of an HTTP/SAPI request context.","commonSituations":"Running PHPUnit or a CLI command that reuses web-oriented formatting code; executing code via a custom runner or daemon where the SAPI does not set REQUEST_TIME_FLOAT; unit tests that invoke the formatter with a minimal/empty $_SERVER array.","solutions":["Check isset($_SERVER['REQUEST_TIME_FLOAT']) (and that it is a float) before calling resourceUsageSinceStartOfRequest(), and fall back to a manually recorded start time when absent","Use the alternative ResourceUsageFormatter::resourceUsage(Duration $duration) API with a Duration you build yourself (e.g. Duration::fromMicroseconds((microtime(true) - $start) * 1000000)) when not in a request context","Only call resourceUsageSinceStartOfRequest() from genuine HTTP request handling code; in CLI code, capture your own start timestamp instead"],"exampleFix":"// before\n$usage = $formatter->resourceUsageSinceStartOfRequest();\n\n// after\nif (isset($_SERVER['REQUEST_TIME_FLOAT']) && is_float($_SERVER['REQUEST_TIME_FLOAT'])) {\n    $usage = $formatter->resourceUsageSinceStartOfRequest();\n} else {\n    $start = microtime(true);\n    // ... work ...\n    $usage = $formatter->resourceUsage(\\SebastianBergmann\\Timer\\Duration::fromMicroseconds((microtime(true) - $start) * 1000000));\n}","handlingStrategy":"try-catch","validationCode":"if (!isset($_SERVER['REQUEST_TIME_FLOAT'])) {\n    // not in an HTTP request context; use a manual start time instead\n}\n","typeGuard":"function hasRequestTimeFloat(): bool\n{\n    return isset($_SERVER['REQUEST_TIME_FLOAT']) && is_float($_SERVER['REQUEST_TIME_FLOAT']);\n}\n","tryCatchPattern":"use SebastianBergmann\\Timer\\TimeSinceStartOfRequestNotAvailableException;\n\ntry {\n    $usage = $formatter->resourceUsageSinceStartOfRequest();\n} catch (TimeSinceStartOfRequestNotAvailableException $e) {\n    $usage = $formatter->resourceUsage(Duration::fromMicroseconds((microtime(true) - $fallbackStart) * 1000000));\n}\n","preventionTips":["Only use resourceUsageSinceStartOfRequest() in SAPI/HTTP request handlers","In CLI scripts, record microtime(true) yourself and use resourceUsage(Duration)","Check the request-context precondition (isset + is_float) before calling","Add a unit test that exercises the fallback path"],"tags":["php","timer","request-context","cli"],"backgroundTag":"missing-env-var","analyzedSha":"3efcdcf2132bd0125edb602c1363011dc5434c3b","analyzedAt":"2026-09-13T22:57:23.791Z","contentChangedAt":"2026-09-13T22:57:23.791Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}