UTC Offset Regex for PHP
/^(?:UTC)?([+-])(2[0-3]|[01][0-9]):([0-5][0-9])$|^(?:UTC)?([+-])(14):(00)$|^(?:UTC|Z|0)$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching utc offset, ported and verified for PHP. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.
Php Implementation
<?php
// UTC Offset
// ReDoS-safe | RegexVault — Localization > Time Formats
define('UTC_OFFSET_PATTERN', '/^(?:UTC)?([+-])(2[0-3]|[01][0-9]):([0-5][0-9])$|^(?:UTC)?([+-])(14):(00)$|^(?:UTC|Z|0)$/');
function validate_utc_offset(string $input): bool {
return (bool) preg_match(UTC_OFFSET_PATTERN, $input);
}
// Example
var_dump(validate_utc_offset("UTC")); // bool(true)Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
UTC | +25:00 |
Z | +05:75 |
+00:00 | 05:30 |
+05:30 | UTC8 |
-05:00 | +5:30 |
+09:30 | +05:3 |
-03:30 | — |
+14:00 | — |
UTC+08:00 | — |
When to use this pattern
This pattern is drawn from the Localization > Time Formats category and carries a ReDoS-safe certification. That matters for PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
+14:00 (Kiribati/Line Islands) is the most positive real-world offset. Any offset beyond ±14:00 is not a valid IANA timezone — reject it.
Technical Notes
Real-world range: -12:00 (US Minor Outlying Islands) to +14:00 (Line Islands, Kiribati). Half-hour and 45-minute offsets exist. Nepal (+05:45) is the only 45-minute zone in common use.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern