/    Sign up×
Community /Pin to ProfileBookmark

Decent Mysql datetime regular expression.

I’ve spent a lot of time trying to find a regular expression matching valid boundaries for MySQL datetimes before I knew anything of regular expressions. Since I had to write my own, I thought I’d share it with those who don’t feel like spending hours picking through very poor alternatives. Most of the examples that I found didn’t check for illegal boundaries, such as 99 days in a month, or more than 12 months in a year.

Mysql Datetime Regular Expression (YYYY-MM-DD HH:MM:SS)

[CODE]^([1-3][0-9]{3,3})-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2][1-9]|3[0-1])s([0-1][0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9])$[/CODE]

This is effectively the same as the readable expression:
(1000-3999)-(1-12)-(1-31) (00-24)?00-59)?00-59)

[B]Broken down:[/B]
Year: ([1-3][0-9]{3,3}) Matches 1000 to 3999, easily changed.
Month: (0?[1-9]|1[0-2]) Matches 1 to 12
Day: (0?[1-9]|[1-2][1-9]|3[0-1]) Mathes 1 to 31
Hour: ([0-1][0-9]|2[0-4]) Matches 00 to 24
Minute: ([0-5][0-9]) Matches 00 to 59
Second: ([0-5][0-9]) Same as above.

[B]Notes: [/B]
The “?” allows for the preceding digit to be optional, ie: “2008-1-22” and “2008-01-22” are both valid.
The “^ denies input before the year, so ” 2008″ or “x2008” is invalid.
The “$” works to deny ending input.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@webgovernorauthorApr 07.2008 — Oops, the section for the day should be:

(0?[1-9]|[1-2][[B]0[/B]-9]|3[0-1]) // Correct

[B]NOT[/B]

(0?[1-9]|[1-2][[B]1[/B]-9]|3[0-1]) // Incorrect

Sorry about that.
Copy linkTweet thisAlerts:
@homemadedigitalJan 27.2010 — Hi there

I had a regex that validates a MySQL date including leap years, so I've run the "time" part of yours into the leapyear-enabled MySQL date regex. Here's a version where the "time" part of the datetime is required:

{^(((d{4})(-)(0[13578]|10|12)(-)(0[1-9]|[12][0-9]|3[01]))|((d{4})(-)(0[469]|11)(-)([0][1-9]|[12][0-9]|30))|((d{4})(-)(02)(-)(0[1-9]|1[0-9]|2[0-8]))|(([02468][048]00)(-)(02)(-)(29))|(([13579][26]00)(-)(02)(-)(29))|(([0-9][0-9][0][48])(-)(02)(-)(29))|(([0-9][0-9][2468][048])(-)(02)(-)(29))|(([0-9][0-9][13579][26])(-)(02)(-)(29)))(s([0-1][0-9]|2[0-4])?[0-5][0-9])?[0-5][0-9]))$}

And here's a version where it's optional:

{^(((d{4})(-)(0[13578]|10|12)(-)(0[1-9]|[12][0-9]|3[01]))|((d{4})(-)(0[469]|11)(-)([0][1-9]|[12][0-9]|30))|((d{4})(-)(02)(-)(0[1-9]|1[0-9]|2[0-8]))|(([02468][048]00)(-)(02)(-)(29))|(([13579][26]00)(-)(02)(-)(29))|(([0-9][0-9][0][48])(-)(02)(-)(29))|(([0-9][0-9][2468][048])(-)(02)(-)(29))|(([0-9][0-9][13579][26])(-)(02)(-)(29)))(s([0-1][0-9]|2[0-4])?[0-5][0-9])?[0-5][0-9]))?$}
Copy linkTweet thisAlerts:
@homemadedigitalJan 27.2010 — Hmmm. I forgot to enclose the regex in code tags so it's been part-converted into smilies. vBulletin should filter its input with a regex that spots regular expressions, and avoid putting smilies in them. Until that day:

Regex for leap-year enabled MySQL date/time validation (time required)
{^(((d{4})(-)(0[13578]|10|12)(-)(0[1-9]|[12][0-9]|3[01]))|((d{4})(-)(0[469]|11)(-)([0][1-9]|[12][0-9]|30))|((d{4})(-)(02)(-)(0[1-9]|1[0-9]|2[0-8]))|(([02468][048]00)(-)(02)(-)(29))|(([13579][26]00)(-)(02)(-)(29))|(([0-9][0-9][0][48])(-)(02)(-)(29))|(([0-9][0-9][2468][048])(-)(02)(-)(29))|(([0-9][0-9][13579][26])(-)(02)(-)(29)))(s([0-1][0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9]))$}

Regex for leap-year enabled MySQL date/time validation (time optional)
{^(((d{4})(-)(0[13578]|10|12)(-)(0[1-9]|[12][0-9]|3[01]))|((d{4})(-)(0[469]|11)(-)([0][1-9]|[12][0-9]|30))|((d{4})(-)(02)(-)(0[1-9]|1[0-9]|2[0-8]))|(([02468][048]00)(-)(02)(-)(29))|(([13579][26]00)(-)(02)(-)(29))|(([0-9][0-9][0][48])(-)(02)(-)(29))|(([0-9][0-9][2468][048])(-)(02)(-)(29))|(([0-9][0-9][13579][26])(-)(02)(-)(29)))(s([0-1][0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9]))?$}
Copy linkTweet thisAlerts:
@emmim44Feb 12.2010 — How can you tweak your regex to confirm leap-year with this format [mm-dd-yyyy hh:mm:ssAM/PM]?
×

Success!

Help @webgovernor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...