验证此格式-"HH:MM"

问题描述:

我正在尝试使用preg_match验证时间输入是否采用以下格式-"HH:MM"

I'm trying to use preg_match to validate that a time input is in this format - "HH:MM"

您可以使用正则表达式进行检查.

You can use regular expressions to check that.

12小时:

preg_match("/^(?:1[012]|0[0-9]):[0-5][0-9]$/", $foo)

24小时:

preg_match("/^(?:2[0-3]|[01][0-9]):[0-5][0-9]$/", $foo)

如果您使用从 01:00 24:59 的24小时制,请使用

If you use a 24-hour clock going from 01:00 to 24:59, use

preg_match("/^(?:2[0-4]|[01][1-9]|10):([0-5][0-9])$/", $foo)