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. -
function validate_date($date)
-
{
-
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))
-
{
-
return true;
-
}
-
However, in javascript the following code does work: -
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/))
-
Can someone please explain this to me and tell me how to go about getting the php regex to behave properly?
Thank you, William.
13 1584
Heya, William.
Instead of using a regular expression, try using strtotime(): -
function makeSafe_date($date)
-
{
-
return date('d-M-Y', strtotime($date));
-
}
-
Another thing is that you should be using preg_match() not ereg().
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.
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
still no luck, any more ideas anyone?
sorry.
william.
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: -
function validateDate($str)
-
{
-
$test = strtotime($str);
-
-
if( ! $test )
-
{
-
return false;
-
}
-
-
return date('d-M-Y', $test);
-
}
-
Then all you have to do is make sure validateDate() doesn't return false.
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: -
function validateDate($str)
-
{
-
$test = strtotime($str);
-
-
if( ! $test )
-
{
-
return false;
-
}
-
-
return date('d-M-Y', $test);
-
}
-
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)
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.
But will this work if the form is
02-Sep-2003
i.e. the month in words and not numbers
??
Thanks.
Heya, William.
But will this work if the form is
02-Sep-2003
i.e. the month in words and not numbers
You betcha.
strtotime() will convert almost every readable and known time/date input.
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: -
function validate_date($date)
-
{
-
$test = strtotime($date);
-
if( !$test )
-
{
-
return false;
-
}
-
date('d-M-Y', $test);
-
}
-
-
if (validate_event($event) == true &&
-
validate_location($location) == true &&
-
validate_date($date) != false &&
-
validate_time($time) == true)
-
Wat is wrong here?? :'(
Thanks, William.
Heya, William.
Compare this: - function validateDate($str)
-
{
-
$test = strtotime($str);
-
-
if( ! $test )
-
{
-
return false;
-
}
-
-
return date('d-M-Y', $test);
-
}
with this: -
function validate_date($date)
-
{
-
$test = strtotime($date);
-
if( !$test )
-
{
-
return false;
-
}
-
date('d-M-Y', $test);
-
}
-
Something's missing....
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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>(+)+),...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
| |