{"record":{"id":"b46ae197e7c29362","repo":"sebastianbergmann/php-timer","slug":"cannot-determine-time-at-which-the-request-started-because-b46ae1","errorCode":null,"errorMessage":"Cannot determine time at which the request started because $_SERVER['REQUEST_TIME_FLOAT'] is not of type float","messagePattern":"Cannot determine time at which the request started because \\$_SERVER\\['REQUEST_TIME_FLOAT'\\] is not of type float","errorType":"exception","errorClass":"TimeSinceStartOfRequestNotAvailableException","httpStatus":null,"severity":"error","filePath":"src/ResourceUsageFormatter.php","lineNumber":49,"sourceCode":"            '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\n    {\n        foreach (self::SIZES as $unit => $value) {\n            if ($bytes >= $value) {\n                return sprintf('%.2f %s', $bytes / $value, $unit);\n            }\n        }","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/sebastianbergmann/php-timer/blob/3efcdcf2132bd0125edb602c1363011dc5434c3b/src/ResourceUsageFormatter.php#L31-L67","documentation":"This exception is thrown when $_SERVER['REQUEST_TIME_FLOAT'] exists but is not of type float. The formatter requires the original float value PHP sets (microsecond-precision timestamp); if something has overwritten it with a string, int, or other type, the library refuses to compute a duration from it to avoid wrong results.","triggerScenarios":"Calling resourceUsageSinceStartOfRequest() after application/framework/test-bootstrap code has coerced or overwritten $_SERVER['REQUEST_TIME_FLOAT'] (e.g. setlocale/string casts, custom middleware writing a string timestamp, test fixtures replacing the superglobal with an int or array value).","commonSituations":"Custom bootstrap scripts that do $_SERVER['REQUEST_TIME_FLOAT'] = time() (an int); frameworks or telemetry tools that stringify superglobals; integration tests seeding $_SERVER manually with wrong types; polyfills or older SAPIs setting an unexpected type.","solutions":["Ensure $_SERVER['REQUEST_TIME_FLOAT'] is the untouched float value PHP provides (remove any code that overwrites it, or restore it via $_SERVER['REQUEST_TIME_FLOAT'] = microtime(true))","Guard with is_float($_SERVER['REQUEST_TIME_FLOAT']) before calling the formatter and fall back to computing Duration yourself from your own start timestamp","Use ResourceUsageFormatter::resourceUsage(Duration) with an explicitly constructed Duration when you cannot trust the superglobal's type"],"exampleFix":"// before\n$_SERVER['REQUEST_TIME_FLOAT'] = time(); // int, breaks the formatter\n$usage = $formatter->resourceUsageSinceStartOfRequest();\n\n// after\n$_SERVER['REQUEST_TIME_FLOAT'] = microtime(true); // float, as PHP SAPI sets it\n$usage = $formatter->resourceUsageSinceStartOfRequest();","handlingStrategy":"type-guard","validationCode":"if (!is_float($_SERVER['REQUEST_TIME_FLOAT'] ?? null)) {\n    $_SERVER['REQUEST_TIME_FLOAT'] = microtime(true); // restore expected float type\n}\n","typeGuard":"function hasFloatRequestTimeFloat(): 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":["Never overwrite $_SERVER['REQUEST_TIME_FLOAT'] with non-float values (time() returns int)","Cast any custom value to float: (float) $value before assigning to the superglobal","Audit bootstrap/middleware/test fixtures that modify $_SERVER","Prefer passing an explicit Duration to resourceUsage() over relying on the superglobal"],"tags":["php","timer","type-mismatch","request-context"],"backgroundTag":"type-mismatch","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"}