How to verify time format with PHP regular expression

PHPz
Release: 2023-06-24 08:20:01
Original
1381 people have browsed it

PHP regular expression method to verify time format

In PHP, we often need to verify whether the time string conforms to the specified format. For example, we might need to verify in a form that the time entered by the user conforms to the format HH:mm:ss. To achieve this functionality, we can use regular expressions.

Regular expression is a tool used to match text patterns. It can match a piece of text through a series of metacharacters and characters. In PHP, we can use the preg_match() function to verify whether a string matches a specified regular expression.

The following is an example of PHP code that uses regular expressions to verify the time format:

Copy after login

In the above code, we first define an input time string $time and a time format Regular expression $pattern. Next, we use the preg_match() function to verify whether $time matches the format of $pattern. If it matches, output "time format is correct", otherwise output "time format is wrong".

The meaning of this regular expression is to match a time string expressed in the format of "hours:minutes:seconds". Specifically, it consists of the following parts:

  • ^ (starting character): Indicates the starting position of the string.
  • ?:: Indicates non-capturing grouping.
  • 01: Matches numbers between 00~19 and 20~23, indicating hours.
  • |: Indicates or.
  • 2[0-3]: Matches numbers between 20 and 23, indicating the number of hours.
  • :: Indicates colon.
  • 0-5: Match numbers between 00~59, indicating minutes and seconds.
  • $: Indicates the end position of the string.

For verification of other time formats, you only need to modify the regular expression. For example, if you want to verify the complete format of the date and time (such as yyyy-MM-dd HH:mm:ss), you can use the following regular expression:

$pattern = "/^(?:(?:(?:1[6-9]|[2-9][0-9])d{2})-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01]))|(?:[12][0-9]{3})-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])))(?: (?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])?$/";
Copy after login

This regular expression will match the yyyy-MM- Time strings in two formats: dd HH:mm:ss and yyyy-MM-dd.

In short, using regular expressions can easily verify the format of the time string, thereby improving the readability and robustness of the code. Hope this article is helpful to you.

The above is the detailed content of How to verify time format with PHP regular expression. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!