Jump to content

How would I check to see if a date fits a certain format


Recommended Posts

JAY6390's suggestion will obviously check that they are all numbers separated by slashes, if you search through the forum, there is a fairly long complex pattern that will go one step further and check the day is valid for the month etc. I think the topic was started by mchl and was quite awhile ago but it's in the Regex forum somewhere.

JAY6390's suggestion will obviously check that they are all numbers separated by slashes, if you search through the forum, there is a fairly long complex pattern that will go one step further and check the day is valid for the month etc. I think the topic was started by mchl and was quite awhile ago but it's in the Regex forum somewhere.

 

Looks like you are the one that wrote it ;)

lol

 

~^(?:0[13578]|1[02])|(?:0[1-9]|[12]\d)/(??:0[1-9]|[12]\d|30)/(?:0[469]|11)|(?:0[1-9]|1\d|2[0-7])\.02)$~

 

Is this what i'm looking at?

If you want to validate the format (what your title states), use the information that  JAY6390 posted. If you then want to validate the date - http://php.net/checkdate

 

Thanks but I realize I would like it to check to make sure that it is a proper date or it is 00/00/0000 in order to state it was a success.

Checking the format first, followed by validating the date, would be impossible to do because of what exactly?

 

Yea I guess I could just draw it out to a few more if statements. Was just hoping that there might be some sort of RegEx that I could use for a one liner I suppose.

The Regex was certainly started by me, it was by no way the best solution and I don't believe we ever managed to make tests for leap years implemented into it. Regular expressions was by no means the best solution, it was simply one that was required by the OP due to certain restrictions. Given a normal script you would of course be better off using proper functions as suggested by PFMaBiSmAd.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.