REGEXVAULTv2.0
Localization/Time Formats
Verified Safe

Time with Fractional Seconds and Timezone Regex for PHP

/^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(?:\.(\d{1,9}))?(?:Z|([+-])(2[0-3]|[01][0-9]):([0-5][0-9]))?$/

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching time with fractional seconds and timezone, 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
<?php
// Time with Fractional Seconds and Timezone
// ReDoS-safe | RegexVault — Localization > Time Formats

define('TIME_WITH_FRACTIONAL_SECONDS_AND_TIMEZONE_PATTERN', '/^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(?:\.(\d{1,9}))?(?:Z|([+-])(2[0-3]|[01][0-9]):([0-5][0-9]))?$/');

function validate_time_with_fractional_seconds_and_timezone(string $input): bool {
    return (bool) preg_match(TIME_WITH_FRACTIONAL_SECONDS_AND_TIMEZONE_PATTERN, $input);
}

// Example
var_dump(validate_time_with_fractional_seconds_and_timezone("12:30:45")); // bool(true)

Test Cases

Matches (Valid)
Rejects (Invalid)
12:30:4524:00:00Z
12:30:45Z12:60:00Z
12:30:45.12312:30:45+25:00
12:30:45.123456789Z12:30:45.1234567890
12:30:45+08:00
00:00:00-05:30

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

India Standard Time (+05:30), Newfoundland (-03:30), Iran (+03:30), and Australia Central (+09:30) use 30-minute offsets. Nepal (+05:45) uses a 45-minute offset. Pattern allows all combinations.

Technical Notes

Capture groups: 1=hour, 2=minute, 3=second, 4=fractional, 5=tz sign, 6=tz hour, 7=tz minute. Z = UTC. Fractional seconds up to 9 places (nanoseconds). Half-hour timezones (+05:30 India, +09:30 Australia) are supported.

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