Home  >  Article  >  Backend Development  >  PHP regular expression practice: matching time format

PHP regular expression practice: matching time format

WBOY
WBOYOriginal
2023-06-22 10:16:401050browse

PHP Regular Expressions in Action: Matching Time Format

Regular expressions are a powerful tool that can be used to find specific patterns in strings and operate on them as needed. The regular expression functions in PHP provide some extremely powerful and flexible tools to easily identify time formats.

In this article, we will learn how to use the regular expression function in PHP to match various time formats. Here are some examples:

  1. Match 12-hour time format

The 12-hour time format is usually used in informal situations such as parties or family events. The time format is similar to "9:30am" or "2:45pm".

To match this time format, you can use the following regular expression:

/^(1[012]|[1-9]):[0-5][0-9](s)?(?i)(am|pm)$/ 

Explanation:

  • ^ represents the beginning of the string.
  • (1[012]|[1-9]) matches any number from 1-12, or matches a single number (1-9).
  • : Represents a colon.
  • 0-5 matches any set of numbers from 00-59.
  • (s)? Indicates that the preceding string can have a space.
  • (?i) means that the following string is not case-sensitive.
  • (am|pm) matches am or pm.
  1. Match 24-hour time format

The 24-hour time format is mainly used in formal occasions, such as in the office or traveling. The time format is similar to "13:30" or "23:45".

To match this time format, you can use the following regular expression:

/^(2[0-3]|[01][0-9]):([0-5][0-9])$/ 

Explanation:

  • ^ represents the beginning of the string.
  • (2[0-3]|01) matches any number between 00-23.
  • : Represents a colon.
  • (0-5) matches any number between 00-59.
  1. Match date and time formats

Date and time formats can be expressed in different ways, common formats include:

  • 10/1/2021 10:30am
  • 2021-10-1 10:30am
  • 10.1.2021 10:30am

To match this time format , you can use the following regular expression:

/^(d{1,2}[-/.]d{1,2}[-/.]d{4})s+(d{1,2}:[0-5][0-9](s)?(?i)(am|pm))$/

Explanation:

  • ^ represents the beginning of the string.
  • (d{1,2}[-/.]d{1,2}[-/.]d{4}) matches something like 10/1/2021, 2021-10-1 or 10.1 Date format for .2021.
  • s represents one or more spaces.
  • (d{1,2}:0-5(s)?(?i)(am|pm)) matches a time format similar to 10:30am or 2:45pm.
  • $ represents the end of the string.

When using regular expressions to match time formats, you should pay attention to the following points:

  • The parentheses () in regular expressions can be used for grouping to capture required part.
  • In regular expressions, square brackets [ ] represent optional character sets, which can be separated by the | symbol.
  • In regular expressions, special characters need to be escaped, such as $ and ^.

Through the examples in this article, I believe you can already use PHP regular expressions to actually match the time format. Hopefully these examples will help you learn and use regular expressions in more depth.

The above is the detailed content of PHP regular expression practice: matching time format. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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 admin@php.cn