Forum Moderators: coopster

Message Too Old, No Replies

Verify date in valid range with regex

         

itledi

12:17 am on Jan 4, 2008 (gmt 0)

10+ Year Member



I am trying to verify that a date is in a valid range.

The acceptable range is within the current date and the last day of the next month.

My regex statement

^[01]?[0-9]\/[0-3]?[0-9]\/20[0-9]{2}$

coopster

2:09 am on Jan 4, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Better yet, how about checkdate() [php.net]?

itledi

6:32 pm on Jan 4, 2008 (gmt 0)

10+ Year Member



The checkdate looks exactly like what I needed, but unfortunatly I have to write this as a regex statement.

I decided the best approach would be to write a different one for each month.

For something like January, I wrote the following, but it dosn't seem to catch an obviously wrong date like "1/32/2008". Might somebody be able to point out what I did wrong?

/^0?1\/(0?[1-9])¦([12][0-9])¦(3[01])\/2008$/

itledi

10:35 pm on Jan 4, 2008 (gmt 0)

10+ Year Member



A-ha!

I figured out why my month sample wasn't working, and then spent the last couple of hours making a regex statement that works for what I need.

It's not perfect. It only works for four digit years, and doesn't account for the dates pre-Gregorian (years 1582 and less).

It also dosn't account for the leap year acceptions. For the current millenium it will think that there is a 29th day in February for 2100, 2200, 2300, 2500, 2600, 2700, 2900 and 3000 when there actually isn't. I couldn't figure out how to exclude those leap year exceptions, but since I'll be long dead before the first exception, I didn't care that much. (Sloppy coding I know.)

Anyways, thought those who come across this post might be interested:

/^(((0?[13578]¦1[02])\/31¦(0?[13-9]¦1[012])\/(29¦30)¦(0?[1-9]¦1[012])\/(0?[1-9]¦1[0-9]¦2[0-8]))\/[0-9]{4}¦0?2\/29\/[0-9]{2}([02468][048]¦[13579][26]))$/