Hi All,
I'm trying to use preg_match to validate that a time input is in this format - "HH:MM"
Hi All,
I'm trying to use preg_match to validate that a time input is in this format - "HH:MM"
preg_match("/(1[012]|0[0-9]):[0-5][0-9]/", $foo)
for 12-hour or
preg_match("/(2[0-3]|[01][0-9]):[0-5][0-9]/", $foo)
for 24-hour.
You can do:
if(preg_match('/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/',$input)) {
// $input is valid HH:MM format.
}
You can do this with a preg_match(), but I'd have to be a very specific regular expression. Otherwise 77:99 would be a valid time.
My suggestion would be to explode()
the string on the :
and then use PHP's date functions to validate the rest and get yourself a timestamp back.