Regular expression for date validation. Just for fun.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$pattern = | |
'/^ | |
# year | |
(?!0000)\pN{2}(?:( | |
(?: | |
# leap year | |
([02468])|[13579])(?(2)[048]|[26]))(?(?<=00)(?(?<=[02468].{3})(?<=[048].{2})|(?<=[26].{2})))| | |
# non-leap year | |
\pN{2} | |
) | |
\. | |
# month | |
(?:(0)|1)(?(3)[1-9]|[0-2]) | |
\. | |
# date | |
(?!00)(?:(?<!02\.)(3)|[0-2]) | |
(?(4) | |
# IV quarter | |
(?(?<=(?:01|03|05|07|08|10|12).{2})[01]|0)| | |
# I-III quarter | |
(?(?<=02.{2}) | |
# February | |
(?(?<=2) | |
# III quarter | |
(?(1) | |
# leap year | |
\pN| | |
# non-leap year | |
[0-8] | |
)| | |
# I-II quarter | |
\pN | |
)| | |
# not February | |
\pN | |
) | |
) | |
$/ux'; | |
var_dump(preg_match($pattern, date('Y.m.d'))); // 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment