473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1584
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

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

Similar topics

9
by: Harry | last post by:
Hi there, does anyone know how I can build a regular expression e.g. for the string.search() function on runtime, depending on the content of variables? Should be something like this: var...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
3
by: LordHog | last post by:
Hello all, I am attempting to create a small scripting application to be used during testing. I extract the commands from the script file I was going to tokenize the each line as one of the...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
5
by: shawnmkramer | last post by:
Anyone every heard of the Regex.IsMatch and Regex.Match methods just hanging and eventually getting a message "Requested Service not found"? I have the following pattern: ^(?<OrgCity>(+)+),...
1
by: sunil | last post by:
Hi, Am writing one C program for one of my module and facing one problem with the regular expression functions provided by the library libgen.h in solaris. In this library we are having two...
1
by: Shawn B. | last post by:
Greetings, I'm using a custom WebBrowser control: http://www.codeproject.com/KB/miscctrl/csEXWB.aspx When I get the DocumentSource of a web page I browsed, and run a regular expression...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.