468,272 Members | 2,066 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

Regular Expression Problem

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
13 1358
pbmods
5,821 Expert 4TB
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
Another thing is that you should be using preg_match() not ereg().
Sep 13 '07 #3
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
azang
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
still no luck, any more ideas anyone?
sorry.
william.
Sep 13 '07 #6
pbmods
5,821 Expert 4TB
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
134 Expert 100+
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
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
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
5,821 Expert 4TB
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
strtotime() will convert almost every readable and known time/date input.
Sep 13 '07 #12
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
5,821 Expert 4TB
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.

Similar topics

11 posts views Thread by Dimitris Georgakopuolos | last post: by
3 posts views Thread by James D. Marshall | last post: by
7 posts views Thread by Billa | last post: by
9 posts views Thread by Pete Davis | last post: by
3 posts views Thread by LordHog | last post: by
25 posts views Thread by Mike | last post: by
5 posts views Thread by shawnmkramer | last post: by
1 post views Thread by sunil | last post: by
1 post views Thread by Shawn B. | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.