Ralph
Ralph

Reputation: 2095

preg_match for php for the date

How to use php to preg_match the string in the following case?

the string will be like 20150923_1111. The date format if this case would be Ymd_Hi.

Just wondering whether it would be achievable to validate the date format.

I just need to make sure the string is in digit and with the same length and does not have to validate the actual time.

Upvotes: 0

Views: 161

Answers (1)

BurningLights
BurningLights

Reputation: 2397

It sounds like you want your regex to check if the string is 8 digits, followed by an underscore, followed by 4 digits. So, you could use preg_match like this:

if (preg_match('/^[0-9]{8}_[0-9]{4}$/', $input)) {
  // Process here
}

The ^ represents the start of the string, [0-9]{x} means look for the quantity x number of digits 0-9 in a row, and the $ represents the end of the string.

Upvotes: 1

Related Questions