Hi all,
As per the title, I need a regular expression in the dd/mm/yyyy to work with the pregmatch function. I have tried online but not having much joy, I keep getting "unknown modifier errors".
Any help MUCH appreciate.
Kind regards and thanks
| SitePoint Sponsor |



Hi all,
As per the title, I need a regular expression in the dd/mm/yyyy to work with the pregmatch function. I have tried online but not having much joy, I keep getting "unknown modifier errors".
Any help MUCH appreciate.
Kind regards and thanks
What are you planning on doing with it? Are you using it for validation or are you needing to capture the individual elements?
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
Made the first part 1 or 2 in case they put 7 for July instead of 07.PHP Code:preg_match("^\d{1,2}/\d{2}/\d{4}^", "datehere");
If you're just needing a rather crude implementation with no chekcing of the actual values, you could use...
If you're wanting, what seems to be, the mac daddy of patterns...PHP Code:function isDate($date){
return 1 === preg_match(
'~^[0-9]{1,2)/[0-9]{1,2)/[0-9]{4)~',
$date
);
}
PHP Code:#dd/MM/yyyy with leap years 100% integrated Valid years : from 1600 to 9999
function isDate($date){
return 1 === preg_match(
'~^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$~',
$date
);
}
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.



Anythony, im sure those regex's work great but its not working for me. I am finishing of a clients existing validation script, so I did not write it from scratch as I would have prefered.
My client is using this script for validation :-
http://www.benjaminkeen.com/software/php_validation/
And the validation function is being called thus: -
Ive been able to use all kinds of regular expresions with it, but for some reason its not likeing dates....PHP Code:$dateRegExp = "^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$";
$rules[] = "required,StartDate,Please enter a valid Start date";
$rules[] = "reg_exp,StartDate, ".$dateRegExp.",i, Please enter your Start Date in the format dd/mm/yyyy";
$errors = validateFields($_POST, $rules);
Last edited by johnuk; Jan 9, 2011 at 10:22. Reason: extra info
Could the space after StartDate, be an issue? Try removing it. Your other attempts would probably work then too.![]()
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
It doesn't this end.
Maybe the library you're using is adding another layer of complexity there?PHP Code:var_dump(
isDate(0)
); # bool(false)
var_dump(
isDate('0')
); # bool(false)
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.



It does on the form howevermay I pm you the link to the form possibly to see what I mean?
Sure
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
The problem lies in the library you're using.
PHP's empty function considers '0' empty.Code:if (!empty($fields[$field_name]) && !preg_match($reg_exp, $fields[$field_name]))
The following things are considered to be empty:
- "" (an empty string)
- 0 (0 as an integer)
- "0" (0 as a string)
- NULL
- FALSE
- array() (an empty array)
- var $var; (a variable declared, but without a value in a class)
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.



Ahhh wonderful ! What is the best way to remedy this?![]()
Personally, for speed, I'd add a minimum length requirement and be done with it for now.
Say, 8?
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
Didn't want to hijack, but I'm curious if anyone has any thoughts on how to lock down the simpler one by allowing -./
Except! They have to match, ya know?
That works, except it will allow things like 10/7.1984PHP Code:~^\d{1,2}[/.-]\d{2}[/.-]\d{4}$~
So possibilities:
10.7.1984
10-7-1984
10/7/1984
Uniformity! Any ideas?
Off the top of my head, I'd normalise the string to something expected then test that. It would keep the RegExp as focused as possible, or rather, simple as possible.
Maybe...
PHP Code:function isDate($date){
$date = str_replace(array('.', '-', '\\'), '/', $date);
return 1 === preg_match(
'~^[0-9]{1,2)/[0-9]{1,2)/[0-9]{4)~',
$date
);
}
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
You could capture the separator in a group and then use a back reference to match what was captured.
For example, ([ab])c\1 will match aca and bca, but not acb or bca.
Do you want to allow any trailing characters after the date? (i.e. there's nothing anchoring the pattern at the end)
Was painful! But I got it nonetheless, for anyone whose interested:
Not extremely useful, just more of a backreferencing test for me? :-pPHP Code:/**
* Matches dates with period, hyphen, or slash as the seperater
* however they have to be the same.
* I.E. 10.5-1975 won't return true, however 10.5.1975 OR 10-5-1975 will
**/
function isDate($date)
{
return (preg_match("~^\d{1,2}([/.-])\d{2}\\1\d{4}$~", $date)==1) ? TRUE : FALSE;
}
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
Bookmarks