Samuz Posted February 16, 2012 Share Posted February 16, 2012 Someone tell me why this returns true when it should return false $date = '1/2/2004'; if(!preg_match('/(\d{1,2})\/(\d{1,2})\/(\d{4})/', $date)) : echo 'false'; else : echo 'true'; endif; } This works all fine and dandy and returns true. But if I were to do something like $date = '1/2/20033'; It'll return false. How can I make it so that it's only limited to the (dd/mm/yyyy) format and nothing else? Quote Link to comment https://forums.phpfreaks.com/topic/257104-date-regex/ Share on other sites More sharing options...
AyKay47 Posted February 16, 2012 Share Posted February 16, 2012 Someone tell me why this returns true when it should return false $date = '1/2/2004'; if(!preg_match('/(\d{1,2})\/(\d{1,2})\/(\d{4})/', $date)) : echo 'false'; else : echo 'true'; endif; } This works all fine and dandy and returns true. But if I were to do something like $date = '1/2/20033'; It'll return false. of course 1/2/20033 will return false, as it should be. Quote Link to comment https://forums.phpfreaks.com/topic/257104-date-regex/#findComment-1318010 Share on other sites More sharing options...
Samuz Posted February 16, 2012 Author Share Posted February 16, 2012 Someone tell me why this returns true when it should return false $date = '1/2/2004'; if(!preg_match('/(\d{1,2})\/(\d{1,2})\/(\d{4})/', $date)) : echo 'false'; else : echo 'true'; endif; } This works all fine and dandy and returns true. But if I were to do something like $date = '1/2/20033'; It'll return false. of course 1/2/20033 will return false, as it should be. Sorry. I meant it returned true. Quote Link to comment https://forums.phpfreaks.com/topic/257104-date-regex/#findComment-1318011 Share on other sites More sharing options...
AyKay47 Posted February 16, 2012 Share Posted February 16, 2012 if you truly want it in the format of dd/mm/yyyy then it will always have two digits as the day and month (e.g 01 not 1) and 4 digits as the year. With that being said, this is what I have come up with. $date = '01/02/2003'; if(!preg_match('~(?:0[1-9]|[12][0-9]|3[01])/(?:0[1-9]|1[012])/(?:19|20)\d{2}~', $date)) echo "false"; else echo "true"; now if you want it in the format of d/m/yyyy , the pattern will need adjusted. Quote Link to comment https://forums.phpfreaks.com/topic/257104-date-regex/#findComment-1318014 Share on other sites More sharing options...
Samuz Posted February 16, 2012 Author Share Posted February 16, 2012 if you truly want it in the format of dd/mm/yyyy then it will always have two digits as the day and month (e.g 01 not 1) and 4 digits as the year. With that being said, this is what I have come up with. $date = '01/02/2003'; if(!preg_match('~(?:0[1-9]|[12][0-9]|3[01])/(?:0[1-9]|1[012])/(?:19|20)\d{2}~', $date)) echo "false"; else echo "true"; now if you want it in the format of d/m/yyyy , the pattern will need adjusted. Thanks for that. But i'm still getting the same problem with $date = '01/02/20033'; Problem being it returning true Quote Link to comment https://forums.phpfreaks.com/topic/257104-date-regex/#findComment-1318017 Share on other sites More sharing options...
AyKay47 Posted February 16, 2012 Share Posted February 16, 2012 if you truly want it in the format of dd/mm/yyyy then it will always have two digits as the day and month (e.g 01 not 1) and 4 digits as the year. With that being said, this is what I have come up with. $date = '01/02/2003'; if(!preg_match('~(?:0[1-9]|[12][0-9]|3[01])/(?:0[1-9]|1[012])/(?:19|20)\d{2}~', $date)) echo "false"; else echo "true"; now if you want it in the format of d/m/yyyy , the pattern will need adjusted. Thanks for that. But i'm still getting the same problem with $date = '1/2/20033'; because 1/2/2004 is not in the format of dd/mm/yyyy. the correct way to write it would be 01/02/2004 if the day and the month are allowed to be a single digit, which it appears you want, the pattern will look like this: $date = '1/2/2003'; if(!preg_match('~(?:[1-9]|[12][0-9]|3[01])/(?:[1-9]|1[012])/(?:19|20)\d{2}~', $date)) echo "false"; else echo "true"; Quote Link to comment https://forums.phpfreaks.com/topic/257104-date-regex/#findComment-1318019 Share on other sites More sharing options...
requinix Posted February 16, 2012 Share Posted February 16, 2012 The expression needs ^ and $ anchors at the beginning and end, respectively. Quote Link to comment https://forums.phpfreaks.com/topic/257104-date-regex/#findComment-1318023 Share on other sites More sharing options...
AyKay47 Posted February 16, 2012 Share Posted February 16, 2012 The expression needs ^ and $ anchors at the beginning and end, respectively. if the date is stand-alone, which judging solely from the example it is, then yes it will, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/257104-date-regex/#findComment-1318028 Share on other sites More sharing options...
Samuz Posted February 16, 2012 Author Share Posted February 16, 2012 Thanks all. Solved. Quote Link to comment https://forums.phpfreaks.com/topic/257104-date-regex/#findComment-1318030 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.