Skip to content

Instantly share code, notes, and snippets.

@Invis1ble
Last active March 15, 2017 09:15
Show Gist options
  • Save Invis1ble/67bb660252251252e938aa23be773717 to your computer and use it in GitHub Desktop.
Save Invis1ble/67bb660252251252e938aa23be773717 to your computer and use it in GitHub Desktop.
Regular expression for date validation. Just for fun.
$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