Php validating 24 hour time format

date, php, time

Solution

This matches 24 hour time including seconds

([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]

If you only want 00 for minutes and seconds, then

([01]?[0-9]|2[0-3]):00:00

Problem

I'm allowing users to pick an hour from `00:00:00` to `23:00:00` and need to check if they submit the right format. Is there a regular expression or php function that validates a 24 hour format e.g. `HH:MM:SS`? I found some regex examples but the 24 hour time I'm validating is always set to `00` for minutes and seconds. Only the hour varies. For example ``` 18:00:00, 23:00:00, 01:00:00 ```

Original source