時間24小時格式正則表達式模式:
([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?
24小時時鐘格式從0-23或00-23開始然後半冒號(:)和由00-59遵循然後(可選擇分號(:),然後按00-59)。
描述:
( # start of group #1
[01]?[0-9] # start with 0-9,1-9,00-09,10-19
| # or
2[0-3] # start with 20-23
) # end of group #1
: # follow by a semi colon (:)
[0-5][0-9] # follow by 0..5 and 0..9, which means 00 to 59
( # start of group #2
: # follow by a semi colon (:)
[0-5][0-9] # follow by 0..5 and 0..9, which means 00 to 59
) # end of group #2
? # optional third part
匹配時間格式:
01:00, 02:00, 13:00,
1:00, 2:00, 13:01,
23:59, 15:00,
00:00, 0:00,
14:34:43, 01:00:00
不匹配時間格式:
24:00 # hour is out of range [0-23]
12:60 # minute is out of range [00-59]
0:0 # invalid format for minute, at least 2 digits
13:1 # invalid format for minute, at least 2 digits
0:00:0 # invalid format for seconds, at least 2 digits
101:00 # hour is out of range [0-23]
實施例:
var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:25')); // OK
var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '25:25')); // KO
var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '25:30')); // KO
var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:2555')); // KO
var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:65')); // KO
var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:59')); // OK
var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:34:43')); // OK
你能舉出你期望的例子嗎? –
下面是一些例子: 14:25 OK, 25:30 KO, 14:65 KO, 14:59 OK, 14點34分43秒OK – bgondy
不回答你的問題,但你應該知道沒有有效時間爲24:* nn * –