Validate that input is in this time format - "HH:MM"

Validate that input is in this time format - "HH:MM"

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Regex Patterns for Time Validation in PHP

Validating a user-provided time string like HH:MM is a common task when building forms, scheduling systems, or logging utilities. While you might be tempted to rely solely on preg_match because it feels like the most direct way to enforce format rules, there is a significant distinction between checking if a string looks like a time and ensuring it represents a valid moment in time.

A regular expression can easily verify that two groups of digits separated by a colon exist. However, regex struggles with semantic validity without becoming incredibly complex. For instance, the pattern /^\d{2}:\d{2}$/ will accept 99:99, 00:60, or 25:75. These are syntactically correct according to the regex but semantically invalid as times of day.

The Limits of Regular Expressions

If your requirement is strictly format validation—ensuring there are exactly two digits, a colon, and two more digits—you can use a straightforward pattern:

$pattern = '/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/';
if (preg_match($pattern, $input)) {
    // Format is valid and within 00:00 to 23:59
}

This pattern breaks down as follows:

  • ^ asserts the start of the string.
  • (0[0-9]|1[0-9]|2[0-3]) matches hours from 00 to 23. It explicitly rejects 24 through 99 by constraining the first digit and the second digit separately.
  • : matches the literal colon separator.
  • [0-5][0-9] matches minutes from 00 to 59.
  • $ asserts the end of the string.

This approach is efficient and runs entirely on the client-side or server-side without external dependencies. However, it requires maintaining a specific logic for hours and minutes within the regex itself. If your application later needs to handle AM/PM formats (e.g., 1:30 PM) or 12-hour clock inputs, this static pattern becomes insufficient.

Leveraging PHP's DateTime Class

For production environments where correctness is paramount, relying on PHP's built-in DateTime class is often superior to raw regex. This method shifts the burden of validation from a string pattern to the language's internal calendar logic.

First, you can attempt to parse the input using DateTime::createFromFormat. If the format string does not match the input data, the function returns false. You must then check this return value explicitly:

$input = '14:30';
$format = 'H:i'; // 24-hour format without leading zeros for single digits, or 'H:i' generally expects HH:MM

// If strict HH:MM is required, the format string must match exactly
$dateTime = DateTime::createFromFormat('H:i', $input);

if ($dateTime !== false && empty($dateTime->format('H:i')) === false) {
    // The input was successfully parsed into a valid date/time object
    echo "Valid time: " . $dateTime->format('H:i');
} else {
    echo "Invalid time format or values.";
}

Note that DateTime::createFromFormat is not just a validator; it constructs an object. This means if the user enters 25:00, the function will fail to create the object because hour 25 does not exist, returning false. This handles edge cases like 00:60 automatically, which would require verbose regex logic to catch.

Trade-offs and Best Practices

When choosing between these methods, consider the context of your application. If you are processing thousands of requests per second where CPU cycles are at a premium, a well-optimized regex might offer a slight performance edge over object instantiation. However, the readability and maintainability of using DateTime usually outweigh minor performance gains in most web applications.

There is also the matter of timezone handling. The HH:MM format represents local time or UTC depending on your application context. If you need to validate against a specific timezone, the regex approach has no mechanism to apply that rule, whereas DateTimeZone can be attached to the DateTime object created via createFromFormat.

In Laravel applications specifically, you might find utility in creating a custom Request Validator rule if this pattern appears frequently across your forms. This keeps your validation logic centralized rather than scattered throughout controllers. You could also explore community resources like https://laravelcompany.com for advanced validation strategies or package recommendations that extend Laravel's core capabilities, though built-in tools are often sufficient for standard time inputs.

Ultimately, the decision rests on whether you need simple format checking or full semantic validation. For strict "HH:MM" requirements where 99:99 must be rejected without error, combining a regex check with DateTime parsing provides the most robust solution.

Our network