By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

Regular Expression Problem

P: 13
Hiya all,

I'm trying to validate a date in php to be in the form 12-Sep-2006 for example. However, for some reason the following regex wont work.
Expand|Select|Wrap|Line Numbers
  1. function validate_date($date)
  2. {
  3. if (ereg("^([012]?\d|3[01])-([Jj][Aa][Nn]|[Ff][Ee][bB]|[Mm][Aa][Rr]|[Aa][Pp] [Rr]|[Mm][Aa][Yy]|[Jj][Uu][Nn]|[Jj][u]l|[aA][Uu][gG]|[Ss][eE][pP]|[oO][Cc] |[Nn][oO][Vv]|[Dd][Ee][Cc])-(19|20)\d\d$", $date))
  4. {
  5.     return true;
  6. }
  7.  
However, in javascript the following code does work:
Expand|Select|Wrap|Line Numbers
  1. if (date.match(/([012]?\d|3[01])-([Jj][Aa][Nn]|[Ff][Ee][bB]|[Mm][Aa][Rr]|[Aa] [Pp][Rr]|[Mm][Aa][Yy]|[Jj][Uu][Nn]|[Jj][u]l|[aA][Uu][gG]|[Ss][eE][pP]|[oO] [Cc]|[Nn][oO][Vv]|[Dd][Ee][Cc])-(19|20)\d\d/))
  2.  
Can someone please explain this to me and tell me how to go about getting the php regex to behave properly?
Thank you, William.
Sep 13 '07 #1
Share this Question
Share on Google+
13 Replies


pbmods
Expert 5K+
P: 5,821
Heya, William.

Instead of using a regular expression, try using strtotime():
Expand|Select|Wrap|Line Numbers
  1. function makeSafe_date($date)
  2. {
  3.      return date('d-M-Y', strtotime($date));
  4. }
  5.  
Sep 13 '07 #2

P: 36
Another thing is that you should be using preg_match() not ereg().
Sep 13 '07 #3

P: 13
Sorry, I have tried using preg_match() but it makes no difference and I do not see how strtodate will help me in any way.

I am using this as a validator to keep dates in the form 13-Sep-2007.

Thanks.
Sep 13 '07 #4

P: 3
I think the first section went wrong. i.e. for the DAY part. try this:

[PHP](0[1-9]|[12][0-9]|3[01])[/PHP]

[LINK TO BLOG REMOVED]- Moderator
Sep 13 '07 #5

P: 13
still no luck, any more ideas anyone?
sorry.
william.
Sep 13 '07 #6

pbmods
Expert 5K+
P: 5,821
Heya, William.

strtotime() can turn *any* valid date string into a Unix timestamp. date() takes a Unix timestamp and turns it into a formatted date.

As long as strtotime() doesn't return false, you know that the User submitted a valid date/time string. You can then use date() to force the date into a format of your choosing.

A slightly better version of makeSafe_date() would look like this:
Expand|Select|Wrap|Line Numbers
  1. function validateDate($str)
  2. {
  3.     $test = strtotime($str);
  4.  
  5.     if( ! $test )
  6.     {
  7.         return false;
  8.     }
  9.  
  10.     return date('d-M-Y', $test);
  11. }
  12.  
Then all you have to do is make sure validateDate() doesn't return false.
Sep 13 '07 #7

ronnil
Expert 100+
P: 134
Heya, William.

strtotime() can turn *any* valid date string into a Unix timestamp. date() takes a Unix timestamp and turns it into a formatted date.

As long as strtotime() doesn't return false, you know that the User submitted a valid date/time string. You can then use date() to force the date into a format of your choosing.

A slightly better version of makeSafe_date() would look like this:
Expand|Select|Wrap|Line Numbers
  1. function validateDate($str)
  2. {
  3.     $test = strtotime($str);
  4.  
  5.     if( ! $test )
  6.     {
  7.         return false;
  8.     }
  9.  
  10.     return date('d-M-Y', $test);
  11. }
  12.  
Then all you have to do is make sure validateDate() doesn't return false.
There's a problem though.... passing 01-02-2007, there's no way of telling if the user meant 1st of February or 2nd of January. It's mostly a problem on international sites i guess. Personally i write how the user should input the date, and trust they are not complete morons (i ofc check that it is a valid date of some sort)
Sep 13 '07 #8

P: 36
You can always convert it to a unix_time stamp validate that that time stamp doesn't equal 0 (false return) then convert it into whatever date you want.
Sep 13 '07 #9

P: 13
But will this work if the form is

02-Sep-2003
i.e. the month in words and not numbers

??

Thanks.
Sep 13 '07 #10

pbmods
Expert 5K+
P: 5,821
Heya, William.

But will this work if the form is

02-Sep-2003
i.e. the month in words and not numbers
You betcha.
Sep 13 '07 #11

P: 36
strtotime() will convert almost every readable and known time/date input.
Sep 13 '07 #12

P: 13
I'm really sorry, this is starting to annoy me now.
Still not working!
please could someone check this out and tell me wats wrong:

Expand|Select|Wrap|Line Numbers
  1. function validate_date($date)
  2. {
  3. $test = strtotime($date);
  4. if( !$test )
  5. {
  6.     return false;
  7. }
  8.         date('d-M-Y', $test);
  9. }
  10.  
Expand|Select|Wrap|Line Numbers
  1. $date = $_POST['date'];
  2.  
Expand|Select|Wrap|Line Numbers
  1.         if (validate_event($event) == true && 
  2.         validate_location($location) == true &&
  3.         validate_date($date) != false &&
  4.         validate_time($time) == true) 
  5.  
Wat is wrong here?? :'(

Thanks, William.
Sep 14 '07 #13

pbmods
Expert 5K+
P: 5,821
Heya, William.

Compare this:
Expand|Select|Wrap|Line Numbers
  1. function validateDate($str)
  2. {
  3.     $test = strtotime($str);
  4.  
  5.     if( ! $test )
  6.     {
  7.         return false;
  8.     }
  9.  
  10.     return date('d-M-Y', $test);
  11. }
with this:
Expand|Select|Wrap|Line Numbers
  1. function validate_date($date)
  2. {
  3. $test = strtotime($date);
  4. if( !$test )
  5. {
  6.     return false;
  7. }
  8.         date('d-M-Y', $test);
  9. }
  10.  
Something's missing....
Sep 14 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.