aeroswat Posted January 18, 2010 Share Posted January 18, 2010 I want to make sure my string fits this format mm/dd/yyyy I want it to make sure that the string fits that exact format. Quote Link to comment https://forums.phpfreaks.com/topic/188952-how-would-i-check-to-see-if-a-date-fits-a-certain-format/ Share on other sites More sharing options...
JAY6390 Posted January 18, 2010 Share Posted January 18, 2010 if(preg_match('%^\d{2}/\d{2}/\d{4}$%', $value) { //Matched code here }else{ //Not matched code here } Quote Link to comment https://forums.phpfreaks.com/topic/188952-how-would-i-check-to-see-if-a-date-fits-a-certain-format/#findComment-997700 Share on other sites More sharing options...
cags Posted January 18, 2010 Share Posted January 18, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/188952-how-would-i-check-to-see-if-a-date-fits-a-certain-format/#findComment-997712 Share on other sites More sharing options...
aeroswat Posted January 19, 2010 Author Share Posted January 19, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/188952-how-would-i-check-to-see-if-a-date-fits-a-certain-format/#findComment-998001 Share on other sites More sharing options...
PFMaBiSmAd Posted January 19, 2010 Share Posted January 19, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/188952-how-would-i-check-to-see-if-a-date-fits-a-certain-format/#findComment-998004 Share on other sites More sharing options...
aeroswat Posted January 19, 2010 Author Share Posted January 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/188952-how-would-i-check-to-see-if-a-date-fits-a-certain-format/#findComment-998006 Share on other sites More sharing options...
PFMaBiSmAd Posted January 19, 2010 Share Posted January 19, 2010 Checking the format first, followed by validating the date, would be impossible to do because of what exactly? Quote Link to comment https://forums.phpfreaks.com/topic/188952-how-would-i-check-to-see-if-a-date-fits-a-certain-format/#findComment-998014 Share on other sites More sharing options...
aeroswat Posted January 19, 2010 Author Share Posted January 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/188952-how-would-i-check-to-see-if-a-date-fits-a-certain-format/#findComment-998017 Share on other sites More sharing options...
JAY6390 Posted January 19, 2010 Share Posted January 19, 2010 if there was it would be massive Quote Link to comment https://forums.phpfreaks.com/topic/188952-how-would-i-check-to-see-if-a-date-fits-a-certain-format/#findComment-998020 Share on other sites More sharing options...
cags Posted January 19, 2010 Share Posted January 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/188952-how-would-i-check-to-see-if-a-date-fits-a-certain-format/#findComment-998062 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.