如果字符串是00:00格式的时间,则获取PHP

Wil*_*ill 0 php regex

我有一个字符串,可能是一个时间或完全可能是其他东西.我想知道字符串的格式是否为00:00.我不需要检查字符串是否是有效时间(即不是像25:98那样),只是字符串是否采用该格式.

She*_*hef 5

正则表达式将是/^\d{2}:\d{2}$/.当且仅当它包含冒号前2位数和冒号后2位数时,才匹配字符串.

这是带有上述正则表达式的PHP if/else条件:

if (preg_match('/^\d{2}:\d{2}$/', $time)) {
    // it is in the time format
} else {
    // it is not in the time format
}
Run Code Online (Sandbox Code Playgroud)