Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/257104-date-regex/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/257104-date-regex/#findComment-1318010
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/257104-date-regex/#findComment-1318011
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/257104-date-regex/#findComment-1318014
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/257104-date-regex/#findComment-1318017
Share on other sites

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";

Link to comment
https://forums.phpfreaks.com/topic/257104-date-regex/#findComment-1318019
Share on other sites

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.